Skip to content

Commit

Permalink
Merge pull request #240 from aikoven/fix-typings
Browse files Browse the repository at this point in the history
Update Selector types for correct extension
  • Loading branch information
alex3165 authored May 10, 2017
2 parents 0c9150e + b0f1565 commit 9806798
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
export as namespace Reselect;

export type Selector<S, R> = (state: S) => R;
export interface OutputSelector<S, R, C> extends Selector<S, R> {

export type OutputSelector<S, R, C> = Selector<S, R> & {
resultFunc: C;
recomputations: () => number;
resetRecomputations: () => number;
}

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

export type OutputParametricSelector<S, P, R, C> = ParametricSelector<S, P, R> & {
resultFunc: C;
recomputations: () => number;
resetRecomputations: () => number;
Expand Down
49 changes: 49 additions & 0 deletions typescript_test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,55 @@ function testSelector() {
);
}

function testNestedSelector() {
type State = {foo: string, bar: number, baz: boolean};

const selector = createSelector(
createSelector(
(state: State) => state.foo,
(state: State) => state.bar,
(foo, bar) => ({foo, bar}),
),
(state: State) => state.baz,
({foo, bar}, baz) => {
const foo1: string = foo;
// typings:expect-error
const foo2: number = foo;

const bar1: number = bar;
// typings:expect-error
const bar2: string = bar;

const baz1: boolean = baz;
// typings:expect-error
const baz2: string = baz;
},
)
}

function testSelectorAsCombiner() {
type SubState = {foo: string};
type State = {bar: SubState};

const subSelector = createSelector(
(state: SubState) => state.foo,
foo => foo,
);

const selector = createSelector(
(state: State) => state.bar,
subSelector,
);

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

// typings:expect-error
const n: number = selector({bar: {foo: ''}});

const s: string = selector({bar: {foo: ''}});
}

type Component<P> = (props: P) => any;

declare function connect<S, P, R>(selector: ParametricSelector<S, P, R>):
Expand Down

0 comments on commit 9806798

Please sign in to comment.