Comments (2)
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.
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)
- Complex transformations HOT 8
- Customizing key used for memoization HOT 4
- Customizing the selector created for each element HOT 1
- Running into issues trying out the examples HOT 6
- Is there any way to customize the equalityCheck? HOT 1
- missing typings for memorize functions
- Add array index as a final argument to createArraySelector combiner HOT 4
- How does memoization work? HOT 5
- Including the key in the mapper function HOT 12
- Incorrect peer dependency HOT 1
- Add typescript type definition file HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reselect-map.