Skip to content

Commit

Permalink
Fix broken tests and add tests for heterogeneous selector parameter t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
benshope committed Aug 7, 2017
1 parent 972a1b1 commit 5cd0696
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type OutputParametricSelector<S, P, R, C> = ParametricSelector<S, P, R> &
resetRecomputations: () => number;
}

/* consistent selector parameter types */
/* homogeneous selector parameter types */

/* one selector */
export function createSelector<S, R1, T>(
Expand Down Expand Up @@ -573,7 +573,7 @@ export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R1
): OutputParametricSelector<S, P, T, (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10, res11: R11, res12: R12) => T>;

/* inconsistent selector parameter types */
/* heterogeneous selector parameter types */

/* one selector */
export function createSelector<S1, R1, T>(
Expand Down
74 changes: 67 additions & 7 deletions typescript_test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function testSelector() {
// typings:expect-error
const num: number = selector({foo: 'bar'});

// typings:expect-error
// allows heterogeneous parameter type input selectors
createSelector(
(state: {foo: string}) => state.foo,
(state: {bar: number}) => state.bar,
Expand Down Expand Up @@ -135,12 +135,62 @@ function testInvalidTypeInCombinator() {
state => state.baz,
(foo: string, bar: number, baz: boolean, fizz: string) => {}
);

// does not allow heterogeneous parameter type
// selectors when the combinator function is typed differently
createSelector(
(state: {testString: string}) => state.testString,
(state: {testNumber: number}) => state.testNumber,
(state: {testBoolean: boolean}) => state.testBoolean,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testNumber: string}) => state.testNumber,
(state: {testStringArray: string[]}) => state.testStringArray,
// typings:expect-error
(foo1: string, foo2: number, foo3: boolean, foo4: string, foo5: string, foo6: string, foo7: string, foo8: number, foo9: string[]) => {
return {foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, foo9};
});

// does not allow a large array of heterogeneous parameter type
// selectors when the combinator function is typed differently
createSelector(
// typings:expect-error
[
(state: {testString: string}) => state.testString,
(state: {testNumber: number}) => state.testNumber,
(state: {testBoolean: boolean}) => state.testBoolean,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testNumber: string}) => state.testNumber,
(state: {testStringArray: string[]}) => state.testStringArray,
], (foo1: string, foo2: number, foo3: boolean, foo4: string, foo5: string, foo6: string, foo7: string, foo8: number, foo9: string[]) => {
return {foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, foo9};
});
}

function testParametricSelector() {
type State = {foo: string;};
type Props = {bar: number};

// allows heterogeneous parameter type selectors
createSelector(
(state: {testString: string}) => state.testString,
(state: {testNumber: number}) => state.testNumber,
(state: {testBoolean: boolean}) => state.testBoolean,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testStringArray: string[]}) => state.testStringArray,
(foo1: string, foo2: number, foo3: boolean, foo4: string, foo5: string, foo6: string, foo7: string, foo8: string, foo9: string[]) => {
return {foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, foo9};
});

const selector = createSelector(
(state: State) => state.foo,
(state: never, props: Props) => props.bar,
Expand Down Expand Up @@ -188,12 +238,6 @@ function testArrayArgument() {
(state: {foo: string}) => state.foo,
]);

// typings:expect-error
createSelector([
(state: {foo: string}) => state.foo,
(state: {bar: number}) => state.bar,
], (foo, bar) => {});

// typings:expect-error
createSelector([
(state: {foo: string}) => state.foo,
Expand Down Expand Up @@ -293,6 +337,22 @@ function testArrayArgument() {
return {foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, bar};
});

// allows a large array of heterogeneous parameter type selectors
const correctlyTypedArraySelector = createSelector([
(state: {testString: string}) => state.testString,
(state: {testNumber: number}) => state.testNumber,
(state: {testBoolean: boolean}) => state.testBoolean,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testString: string}) => state.testString,
(state: {testStringArray: string[]}) => state.testStringArray,
], (foo1: string, foo2: number, foo3: boolean, foo4: string, foo5: string,
foo6: string, foo7: string, foo8: string, foo9: string[]) => {
return {foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, foo9};
});

// typings:expect-error
parametric({foo: 'fizz'});

Expand Down

0 comments on commit 5cd0696

Please sign in to comment.