Skip to content

Better typings, No more generic Function type. User's can specify input and output types. #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 9, 2019

Conversation

Dudeonyx
Copy link
Contributor

@Dudeonyx Dudeonyx commented Jan 7, 2019

Over at the redux-starter-kit repo which uses this lib, we are planning to switch to Typescript but the generic Function type selectors selectorator returns break type safety, so I decided to either switch to reselect or help you guys with better typings.

So basically this update allows Users to supply Input & Output type parameters. Even without those it will try to infer the output type from the getComputedValue function.

createSelector<State, Output>

Also overloads were added to handle different parameters,

For selectors taking multiple parameters the Input type can be an array
e.g

createSelector<[string, number, string[], boolean ], number>

will give the selector this type signature

(state_0: string, state_1: number, state_2: string[], state_3: boolean) => number

Examples

import createSelector from "selectorator";
   
interface State {
   foo: {
        bar: string;
    };
    baz: string;
 };
                            // State is input type, string is output type
   const getBarBaz = createSelector<State, string>(["foo.bar", "baz"], (bar, baz) => {
     return `${bar} ${baz}`;
   });
   
    // getBarBaz() has type signature: (state: State) => string;
   
   const getBarBaz2 = createSelector<any, string>(["foo.bar", "baz"], (bar, baz) => {
       return `${bar} ${baz}`;
   });
   
    // getBarBaz2() has type signature: (state: any) => string;
   
   const getBarBaz3 = createSelector(["foo.bar", "baz"], (bar, baz) => {
       return `${bar} ${baz}`;
   });
  
    // getBarBaz3() has type signature: (state: any) => any;
  
     const getBarBaz4 = createSelector(["foo.bar", "baz", { path: 0, argIndex: 2 }], (bar, baz) => {
        return `${bar} ${baz}`;
    });
   
    // getBarBaz4() has type signature: (...state: any[]) => any;
   
   const getBarBazQux5 = createSelector<[State, string[]],string>(["foo.bar", "baz", { path: 0, argIndex: 2 }], (bar, baz) => {
      return `${bar} ${baz}`;
    });
   
    // getBarBaz5() has type signature: (state_0: State, state_1: string[]) => string;
   
    const getStructuredBarBaz = createSelector({
       barBaz: getBarBaz,
    }});  

  // getStructuredBarBaz() has type signature: (state: any) => ({ barBaz: string });
  

@planttheidea
Copy link
Owner

This is great. I'm pretty new to TypeScript, so the generics were based on my limited knowledge. I'll merge and release this tonight!

@Dudeonyx
Copy link
Contributor Author

Dudeonyx commented Jan 7, 2019

In that case I'll try to update the readme to document these changes

@planttheidea
Copy link
Owner

Lemme know if you want me to hold off or additional changes to the README or if you're cool with me releasing.

@Dudeonyx
Copy link
Contributor Author

Dudeonyx commented Jan 8, 2019

Done

@planttheidea
Copy link
Owner

Alright cool. Unfortunately I just got to work so I won't be about to do the release until tonight, but it'll happen soon as I'm home.

@planttheidea planttheidea merged commit 6699d8d into planttheidea:master Jan 9, 2019
@planttheidea
Copy link
Owner

Alright, this has been released as part of 4.0.2. Hopefully this helps with redux-starter-kit!

@Dudeonyx
Copy link
Contributor Author

Alright, been really busy for the past few days, it's great that this was merged, let me know if you notice any issues with the typings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants