Giter Club home page Giter Club logo

Comments (9)

markerikson avatar markerikson commented on July 17, 2024

I'm kinda confused. Can you give an example of when you feel you need to explicitly specify the type?

Reselect's current types are designed to infer as much as possible, and you generally shouldn't be explicitly providing generic arguments to createSelector.

from reselect.

AlbertFib avatar AlbertFib commented on July 17, 2024

At the moment, we have recently migrated a large project from flow to typescript and the typing of the code leaves much to be desired. Automatic type inference often outputs any. By explicitly specifying the types for the combiner, we also fix the composition of selectors inside createSelector

from reselect.

markerikson avatar markerikson commented on July 17, 2024

You're supposed to use Reselect the other way around :)

Type the input function arguments correctly, and everything else should get inferred right.

In this case, go back to the definitions of selectCheckoutCartGroups, selectCheckoutCarts , etc, and make sure that their input functions have the right types, like:

const selectCheckoutCartGroups = createSelector(
  (state: RootState) => state.some.cart,
  (state: RootState, someOtherArg: number) => someOtherArg,
  (cart, someOtherArg) => // whatever here
)

// selectCheckoutCartGroups is now typed as;
// (state: RootState, someOtherArg: number) => whateverYourReturnValueIs

Do that for each of those selectors, and the types will propagate through correctly.

from reselect.

markerikson avatar markerikson commented on July 17, 2024

While we're at it, what is this stateCreator function?

from reselect.

AlbertFib avatar AlbertFib commented on July 17, 2024

While we're at it, what is this stateCreator function?

Probably a bad example
This is a legacy code, there is nothing good there, unfortunately :)

from reselect.

AlbertFib avatar AlbertFib commented on July 17, 2024

You're supposed to use Reselect the other way around :)

Type the input function arguments correctly, and everything else should get inferred right.

In this case, go back to the definitions of selectCheckoutCartGroups, selectCheckoutCarts , etc, and make sure that their input functions have the right types, like:

const selectCheckoutCartGroups = createSelector(
  (state: RootState) => state.some.cart,
  (state: RootState, someOtherArg: number) => someOtherArg,
  (cart, someOtherArg) => // whatever here
)

// selectCheckoutCartGroups is now typed as;
// (state: RootState, someOtherArg: number) => whateverYourReturnValueIs

Do that for each of those selectors, and the types will propagate through correctly.

Yes, this is the ideal option that we are striving for. Unfortunately, now at the moment there is a need for part of the team to type explicitly. Even the described types do not always give the expected result. To increase readability, we would like to remove an extra parameter that is duplicated in the type. Of the options was to redefine them locally or come here with a suggestion :)

from reselect.

markerikson avatar markerikson commented on July 17, 2024

@AlbertFib : why do you need to "type explicitly"?

from reselect.

AlbertFib avatar AlbertFib commented on July 17, 2024

@AlbertFib : why do you need to "type explicitly"?

In some places we consciously want to type explicitly and control the return type of the created selector, and not rely on automatic output, so that if the selector changes, we get a type error in this place.

from reselect.

markerikson avatar markerikson commented on July 17, 2024

Honestly, you're bringing the pain upon yourself :)

We designed Reselect's types to infer as much as possible, so that you don't have to write all the types out explicitly.

Given that, I don't think we're going to make a change here.

from reselect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.