Coder Social home page Coder Social logo

Comments (2)

heyimalex avatar heyimalex commented on May 21, 2024 1

Here's an update:

Ideally the list and map selectors would be usable without specifying any generics.

import { List } from "immutable";
import { createListSelector } from "reselect-map";

type State = {
  items: List<number>;
  mul: number;
};

const selector = createListSelector(
  (state: State) => state.items,
  (state: State) => state.mul,
  (elem, mul) => `value is ${elem * mul}`
);
// The created selector's output type is inferred to be List<string>.

However, this doesn't look like it's possible. I can infer the element type from a specific mapable type, ie: List<E>, but inferring the return type looks like it can't be done as List<E>.map<T>'s return type is itself generic wrt to the return type of the mapper function T.

So we're going to have to specify some types. But we should be able to get away with only specifying one. This leads to more of a usability decision than a technical one, and really we're kind of going around the type system. But because we can't define the result type in terms of the combiner's return type, that's kinda a given anyway.

Anyway, the gist is that you need to annotate the return type of your first selector to Mapper<ElementType,FinalResultType> for lists and KeyMapper<ElementType,KeyType,FinalResultType> for maps. I've published a branch that has these changes for anyone interested. The final result is maybe more convoluted than it needs to be, but it looks like it works so I'm calling it a day.

I'm thinking of adding support directly for immutable.js, since that's how these selectors are usually used, but I'm not sure about the implications for importing (like whether the end user needs immutable js installed or else the regular typings won't work at all). More work to come...

from reselect-map.

heyimalex avatar heyimalex commented on May 21, 2024

When we created the original typings it wasn't possible, but maybe that's changed. I spent twenty minutes thinking about it just now and didn't get anywhere, so if you need this implemented you should take a crack at it. Check #9 for details on how we generated the original types if you get that far. The crux is that you need to define a generic type that takes a mapping function and then returns itself.

I think it's possible if you're willing to annotate both types, and maybe you can use infer somehow to make that unnecessary. I kinda like playing solve-the-type-puzzle so I may get to this next week.

from reselect-map.

Related Issues (12)

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.