Skip to content

Commit

Permalink
Typings tweaks
Browse files Browse the repository at this point in the history
* Allow selectors accept extra arguments
* Remove `...rest` versions of `createSelector`
* Update `createStructuredSelector` to use Mapped types
  • Loading branch information
aikoven committed Dec 12, 2016
1 parent dd1003f commit 8c48a39
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"mocha": "^2.2.5",
"ncp": "^2.0.0",
"nyc": "^6.4.0",
"typescript": "^2.1.1",
"typescript": "^2.1.4",
"typings-tester": "^0.2.0"
}
}
331 changes: 271 additions & 60 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type Selector<S, R> = (state: S) => R;

export type ParametricSelector<S, P, R> = (state: S, props: P) => R;
export type ParametricSelector<S, P, R> = (state: S, props: P,
...args: any[]) => R;


/* one selector */
Expand Down Expand Up @@ -143,30 +144,131 @@ export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, T>(
res7: R7, res8: R8) => T,
): ParametricSelector<S, P, T>;

/* rest */
export function createSelector<S, T>(
selector1: Selector<S, any>,
selector2: Selector<S, any>,
selector3: Selector<S, any>,
selector4: Selector<S, any>,
selector5: Selector<S, any>,
selector6: Selector<S, any>,
selector7: Selector<S, any>,
selector8: Selector<S, any>,
...rest: Function[],
/* nine selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, T>(
selector1: Selector<S, R1>,
selector2: Selector<S, R2>,
selector3: Selector<S, R3>,
selector4: Selector<S, R4>,
selector5: Selector<S, R5>,
selector6: Selector<S, R6>,
selector7: Selector<S, R7>,
selector8: Selector<S, R8>,
selector9: Selector<S, R9>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, T>(
selector1: ParametricSelector<S, P, R1>,
selector2: ParametricSelector<S, P, R2>,
selector3: ParametricSelector<S, P, R3>,
selector4: ParametricSelector<S, P, R4>,
selector5: ParametricSelector<S, P, R5>,
selector6: ParametricSelector<S, P, R6>,
selector7: ParametricSelector<S, P, R7>,
selector8: ParametricSelector<S, P, R8>,
selector9: ParametricSelector<S, P, R9>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9) => T,
): ParametricSelector<S, P, T>;

/* ten selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, T>(
selector1: Selector<S, R1>,
selector2: Selector<S, R2>,
selector3: Selector<S, R3>,
selector4: Selector<S, R4>,
selector5: Selector<S, R5>,
selector6: Selector<S, R6>,
selector7: Selector<S, R7>,
selector8: Selector<S, R8>,
selector9: Selector<S, R9>,
selector10: Selector<S, R10>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10) => T,
): Selector<S, T>;
export function createSelector<S, P, T>(
selector1: ParametricSelector<S, P, any>,
selector2: ParametricSelector<S, P, any>,
selector3: ParametricSelector<S, P, any>,
selector4: ParametricSelector<S, P, any>,
selector5: ParametricSelector<S, P, any>,
selector6: ParametricSelector<S, P, any>,
selector7: ParametricSelector<S, P, any>,
selector8: ParametricSelector<S, P, any>,
...rest: Function[],
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, T>(
selector1: ParametricSelector<S, P, R1>,
selector2: ParametricSelector<S, P, R2>,
selector3: ParametricSelector<S, P, R3>,
selector4: ParametricSelector<S, P, R4>,
selector5: ParametricSelector<S, P, R5>,
selector6: ParametricSelector<S, P, R6>,
selector7: ParametricSelector<S, P, R7>,
selector8: ParametricSelector<S, P, R8>,
selector9: ParametricSelector<S, P, R9>,
selector10: ParametricSelector<S, P, R10>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10) => T,
): ParametricSelector<S, P, T>;

/* eleven selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, T>(
selector1: Selector<S, R1>,
selector2: Selector<S, R2>,
selector3: Selector<S, R3>,
selector4: Selector<S, R4>,
selector5: Selector<S, R5>,
selector6: Selector<S, R6>,
selector7: Selector<S, R7>,
selector8: Selector<S, R8>,
selector9: Selector<S, R9>,
selector10: Selector<S, R10>,
selector11: Selector<S, R11>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10, res11: R11) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, T>(
selector1: ParametricSelector<S, P, R1>,
selector2: ParametricSelector<S, P, R2>,
selector3: ParametricSelector<S, P, R3>,
selector4: ParametricSelector<S, P, R4>,
selector5: ParametricSelector<S, P, R5>,
selector6: ParametricSelector<S, P, R6>,
selector7: ParametricSelector<S, P, R7>,
selector8: ParametricSelector<S, P, R8>,
selector9: ParametricSelector<S, P, R9>,
selector10: ParametricSelector<S, P, R10>,
selector11: ParametricSelector<S, P, R11>,
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10, res11: R11) => T,
): ParametricSelector<S, P, T>;

/* twelve selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, T>(
selector1: Selector<S, R1>,
selector2: Selector<S, R2>,
selector3: Selector<S, R3>,
selector4: Selector<S, R4>,
selector5: Selector<S, R5>,
selector6: Selector<S, R6>,
selector7: Selector<S, R7>,
selector8: Selector<S, R8>,
selector9: Selector<S, R9>,
selector10: Selector<S, R10>,
selector11: Selector<S, R11>,
selector12: Selector<S, R12>,
combiner: (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,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, T>(
selector1: ParametricSelector<S, P, R1>,
selector2: ParametricSelector<S, P, R2>,
selector3: ParametricSelector<S, P, R3>,
selector4: ParametricSelector<S, P, R4>,
selector5: ParametricSelector<S, P, R5>,
selector6: ParametricSelector<S, P, R6>,
selector7: ParametricSelector<S, P, R7>,
selector8: ParametricSelector<S, P, R8>,
selector9: ParametricSelector<S, P, R9>,
selector10: ParametricSelector<S, P, R10>,
selector11: ParametricSelector<S, P, R11>,
selector12: ParametricSelector<S, P, R12>,
combiner: (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,
): ParametricSelector<S, P, T>;


/* array argument */

/* one selector */
Expand Down Expand Up @@ -283,34 +385,154 @@ export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, T>(
res7: R7) => T,
): ParametricSelector<S, P, T>;

/* eight and more selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, SS extends [
Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>
] & {[index: number]: Selector<S, any>}, T>(
selectors: SS,
/* eight selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, ...rest: any[]) => T,
res7: R7, res8: R8) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, SS extends [
ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>
] & {[index: number]: ParametricSelector<S, P, any>}, T>(
selectors: SS,
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, T>(
selectors: [ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, ...rest: any[]) => T,
res7: R7, res8: R8) => T,
): ParametricSelector<S, P, T>;

/* nine selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>,
Selector<S, R9>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, T>(
selectors: [ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>,
ParametricSelector<S, P, R9>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9) => T,
): ParametricSelector<S, P, T>;

/* ten selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>,
Selector<S, R9>,
Selector<S, R10>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, T>(
selectors: [ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>,
ParametricSelector<S, P, R9>,
ParametricSelector<S, P, R10>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10) => T,
): ParametricSelector<S, P, T>;

/* eleven selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>,
Selector<S, R9>,
Selector<S, R10>,
Selector<S, R11>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10, res11: R11) => T,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, T>(
selectors: [ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>,
ParametricSelector<S, P, R9>,
ParametricSelector<S, P, R10>,
ParametricSelector<S, P, R11>],
combiner: (res1: R1, res2: R2, res3: R3, res4: R4, res5: R5, res6: R6,
res7: R7, res8: R8, res9: R9, res10: R10, res11: R11) => T,
): ParametricSelector<S, P, T>;

/* twelve selectors */
export function createSelector<S, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, T>(
selectors: [Selector<S, R1>,
Selector<S, R2>,
Selector<S, R3>,
Selector<S, R4>,
Selector<S, R5>,
Selector<S, R6>,
Selector<S, R7>,
Selector<S, R8>,
Selector<S, R9>,
Selector<S, R10>,
Selector<S, R11>,
Selector<S, R12>],
combiner: (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,
): Selector<S, T>;
export function createSelector<S, P, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, T>(
selectors: [ParametricSelector<S, P, R1>,
ParametricSelector<S, P, R2>,
ParametricSelector<S, P, R3>,
ParametricSelector<S, P, R4>,
ParametricSelector<S, P, R5>,
ParametricSelector<S, P, R6>,
ParametricSelector<S, P, R7>,
ParametricSelector<S, P, R8>,
ParametricSelector<S, P, R9>,
ParametricSelector<S, P, R10>,
ParametricSelector<S, P, R11>,
ParametricSelector<S, P, R12>],
combiner: (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,
): ParametricSelector<S, P, T>;


Expand Down Expand Up @@ -349,22 +571,11 @@ export function createSelectorCreator<O1, O2, O3>(
...rest: any[],
): typeof createSelector;


export function createStructuredSelector<S, T>(
selectors: {[key: string]: Selector<S, any>},
selectors: {[K in keyof T]: Selector<S, T[K]>},
selectorCreator?: typeof createSelector,
): Selector<S, T>;
export function createStructuredSelector<S, P, T>(
selectors: {[key: string]: ParametricSelector<S, P, any>},
selectors: {[K in keyof T]: ParametricSelector<S, P, T[K]>},
selectorCreator?: typeof createSelector,
): ParametricSelector<S, P, T>;

// todo: for TypeScript 2.1:
// export function createStructuredSelector<S, T>(
// selectors: {[K in keyof T]: Selector<S, T[K]>},
// selectorCreator?: typeof createSelector,
// ): Selector<S, T>;
// export function createStructuredSelector<S, P, T>(
// selectors: {[K in keyof T]: ParametricSelector<S, P, T[K]>},
// selectorCreator?: typeof createSelector,
// ): ParametricSelector<S, P, T>;
Loading

0 comments on commit 8c48a39

Please sign in to comment.