reselect icon indicating copy to clipboard operation
reselect copied to clipboard

TypeScript typings for reselect?

Open cveld opened this issue 11 years ago • 29 comments

Do you provide TypeScript typings for reselect?

cveld avatar Oct 17 '15 20:10 cveld

I would be very happy if someone decided to create Typescript definitions. We did make some changes for the 1.0.0 release that should make them possible (#27).

ellbee avatar Oct 18 '15 10:10 ellbee

It would also be interesting to see Flow types as well. I'm not sure whether the state of Flow is far enough along to cover all the ES6 we use. I wonder whether it's possible to generate one from the other.

faassen avatar Oct 19 '15 10:10 faassen

Need this as well, right now I'm debugging errors since 1/2 hour in my code due to incorrect manual types I added when composing selectors. If the compiler would know then it would be a ton of help (plus would reduce a lot of the manual effort needed to propagate the types). I would like to help exept that I'm pretty sure I don't know enough about the typing system to write the types definitions.

inakianduaga avatar Dec 18 '15 17:12 inakianduaga

for myself i created just most stupid definition:

declare module Reselect {
    function createSelector(...args: any[]): any;
}

declare module "reselect" {
    export = Reselect;
}

nizsheanez avatar Dec 20 '15 15:12 nizsheanez

How about:

declare module "reselect" {
    type Selector<TInput, TOutput> = (arg1: TInput, arg2?: any) => TOutput;
    export function createSelector<TInput, TOutput, T1>(selector1: Selector<TInput, T1>, combiner: (arg1: T1) => TOutput): Selector<TInput, TOutput>;
    export function createSelector<TInput, TOutput, T1, T2>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, combiner: (arg1: T1, arg2: T2) => TOutput): Selector<TInput, TOutput>;
    export function createSelector<TInput, TOutput, T1, T2, T3>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, combiner: (arg1: T1, arg2: T2, arg3: T3) => TOutput): Selector<TInput, TOutput>;
    /* etc */
}

frankwallis avatar Jan 20 '16 13:01 frankwallis

@frankwallis , I just added your type definition to a project with some 40+ selectors. It seems to work pretty well.

For anyone who don't like to type a lot, here's some more of the "etc" part:

declare module Reselect {

    type Selector<TInput, TOutput> = (state: TInput, props?: any) => TOutput;
    function createSelector<TInput, TOutput, T1>(selector1: Selector<TInput, T1>, combiner: (arg1: T1) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, combiner: (arg1: T1, arg2: T2) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, combiner: (arg1: T1, arg2: T2, arg3: T3) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6, T7>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, selector7: Selector<TInput, T7>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6, T7, T8>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, selector7: Selector<TInput, T7>, selector8: Selector<TInput, T8>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6, T7, T8, T9>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, selector7: Selector<TInput, T7>, selector8: Selector<TInput, T8>, selector9: Selector<TInput, T9>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, selector7: Selector<TInput, T7>, selector8: Selector<TInput, T8>, selector9: Selector<TInput, T9>, selector10: Selector<TInput, T10>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => TOutput): Selector<TInput, TOutput>;
    function createSelector<TInput, TOutput, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(selector1: Selector<TInput, T1>, selector2: Selector<TInput, T2>, selector3: Selector<TInput, T3>, selector4: Selector<TInput, T4>, selector5: Selector<TInput, T5>, selector6: Selector<TInput, T6>, selector7: Selector<TInput, T7>, selector8: Selector<TInput, T8>, selector9: Selector<TInput, T9>, selector10: Selector<TInput, T10>, selector11: Selector<TInput, T11>, combiner: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11) => TOutput): Selector<TInput, TOutput>;

    function defaultMemoize(func:Function, equalityCheck:any): any;

    function createSelectorCreator(memoize:Function, ...memoizeOptions:any[]): any;

    function createStructuredSelector(inputSelectors:any, selectorCreator:any): any;

}

declare module "reselect" {
    export = Reselect
}

geon avatar Jan 26 '16 08:01 geon

Yes they're working pretty well for me too, and providing type-inference in the 'combiner' function which is really useful. Thanks for filling out more of the overloads, I've submitted a PR to DefinitelyTyped.

frankwallis avatar Jan 26 '16 21:01 frankwallis

@frankwallis works nicely, thanks a bunch!

inakianduaga avatar Jan 27 '16 09:01 inakianduaga

Hey, this is great, thanks everyone! I'll add a link to here from the readme.

Do you think you could let me know if they accept the PR to DefinitelyTyped please? I'll update the readme to point there instead if that happens.

ellbee avatar Jan 27 '16 22:01 ellbee

Ok will do - I'm not sure on the best way to get it merged but it may help if the PR has been 'approved' by a member of the reselect project...

Until it is merged if you are using tsd you can manually put the PR commit hash in tsd.json to install this declaration file:

    "reselect/reselect.d.ts": {
      "commit": "7ee8c031f981bfb4fd1dd511d3edf598ab96e5bb"
    },

frankwallis avatar Jan 28 '16 13:01 frankwallis

That PR has now been merged.

frankwallis avatar Jan 29 '16 10:01 frankwallis

Thanks

ellbee avatar Jan 29 '16 12:01 ellbee

Would you guys be willing to include the typings in the NPM package? The main benefit being that the typings can be managed directly in this repo, and users do not have to go to a third-party source to get typings for this lib. I would be more than willing to implement this.

https://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages

ianks avatar Mar 14 '16 17:03 ianks

@ianks Yes, please do!

ellbee avatar Mar 14 '16 19:03 ellbee

After having used the typing for a while, I noticed it isn't playing well with connectfrom react-redux.

Since the props argument in the Selector<> function is any, all component wrappers created by connect are inferred to have the props be any. That sucks.

So I'm experimenting with this instead:


    type Selector<TInput, TProps, TOutput> = (state: TInput, props?: TProps) => TOutput;

    function createSelector<TInput, TProps, TOutput, T1>(selector1: Selector<TInput, TProps, T1>, combiner: (arg1: T1) => TOutput): Selector<TInput, TProps, TOutput>;
    function createSelector<TInput, TProps, TOutput, T1, T2>(selector1: Selector<TInput, TProps, T1>, selector2: Selector<TInput, TProps, T2>, combiner: (arg1: T1, arg2: T2) => TOutput): Selector<TInput, TProps, TOutput>;

Seems to work fine. Any comments?

geon avatar Apr 11 '16 15:04 geon

Thanks for the suggestion. I don't know Typescript well enough to be confident in commenting.

@ianks @frankwallis @geon: do any of you want commit access to Reselect to take care of Typescript issues?

ellbee avatar Apr 12 '16 17:04 ellbee

@geon that looks good. can you create a minimal example case for this (just to show that that types are retained from connect)?

ianks avatar Apr 12 '16 18:04 ianks

@ianks

Here's a semi-realistic example of what our code looks like:

delete_button_component.ts


export type DeleteButtonProps = DeleteButtonStateProps & DeleteButtonDispatchProps;

export type DeleteButtonStateProps = {
    disabled: boolean
}

export type DeleteButtonDispatchProps = {
    onClick: event => void
}

export function deleteButton({
    disabled,
    onClick
}: DeleteButtonProps) {

    return DOM.button({
        disabled,
        onClick
    }, 'Delete');
}

delete_button_container.ts


type DeleteButtonContainerProps = {
    itemId: string
}

const mapStateToProps = createSelector<
    // NOTE! 3 types for the input/output instead of 2 as before.
    RootState, DeleteButtonContainerProps, DeleteButtonStateProps,
    boolean
> (
    (state, props) => !!state.items[props.itemId],
    (itemExists: boolean) => ({
        disabled: !itemExists
    })
);

function mapDispatchToProps (dispatch: ReactRedux.Dispatch, props: DeleteButtonContainerProps): DeleteButtonDispatchProps {

    return {
        onClick: event => dispatch(actions.deleteItem(props.itemId))
    };
}

export const DeleteButton = connect(mapStateToProps, mapDispatchToProps)(deleteButton);

Now, calling the container with anything else than DeleteButtonContainerProps will not compile.

Using it:


function render () {

    return DOM.div({}
        SomeItemView({
            foo: 'foo',
            bar: 'bar',
            itemId: theItemId
        }),
        DeleteButton({
            itemId: theItemId
        })
    );
}

We don't use jsx, so I don't really know how to write a more idiomatic example. Seems like nobody else use the DOM api manually. :P

This code has obviously never run, so I'm sure there are some bugs in it. But I think you get the gist.

Without the props typing I suggested, DeleteButton would accept any extra and missing props.

geon avatar Apr 13 '16 06:04 geon

@ellbee , I'm not sure I should have commit access. I still feel like a react noob. I've only been using it since christmas.

geon avatar Apr 13 '16 06:04 geon

I need to just be sure that it does not become necessary to specify the type parameters when calling createSelector, currently I am using it like this (simplified):

interface GlobalState {
   products: { [id: number] : Product; }
   clients: { [id: string] : Client; }
}

interface MyComponentProps {
   clientId: string;
}
const clientRepoSelector = (state: GlobalState) => state.clients;
const clientIdSelector = (state: GlobalState, props: MyComponentProps) => props.clientId;

const mapStateToProps = createSelector(
   clientRepoSelector,
   clientIdSelector,
   (clientRepo, clientId) => clientRepo[clientId]
);

So I specify the shape of the global state and the inputs to the functions and then get type inference in the 'combiner' function and the resulting selectors. The type inference feeds into other selectors when you start combining them.

I would be quite unhappy if I had to start specifying all the type parameters whenever I called createSelector, so I would like to check that first.

Can I also ask if you are using connect as a decorator or as a plain function as I think that can also affect the type of the resulting component.

frankwallis avatar Apr 13 '16 09:04 frankwallis