-
Notifications
You must be signed in to change notification settings - Fork 670
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
Destructuring or spreading createSelector() args as arrays TypeScript error #340
Comments
This is a funky issue. I was able to find a workaround for your last scenario, but this doesn't help if the number of values returned from export type PS = ParametricSelector<any, {}, any>;
const selectorArray = concat<PS>(regularSelectorA, selectorFactoryFactory('B', 'C'));
// or if you want to use spread
const selectorArray = [regularSelectorA, ...selectorFactoryFactory('B', 'C')] as PS[];
createSelector(
selectorArray as [PS, PS, PS],
(a, b, c) => ({ a, b, c })); |
But I also think your second case is not a const add2 = (a: number, b: number) => a + b;
add2(...[2, 3]);
// [ts] Expected 2 arguments, but got 0 or more. side note, here is an extremely weird way of fixing this, but if you're using const add2 = (a: number, b: number) => a + b;
(add2 as any)(...[2, 3]); |
Looks like there's already even been some prior thinking on this? |
const add2 = (a: number, b: number) => a + b;
add2(...[2, 3]);
// [ts] Expected 2 arguments, but got 0 or more. I think the issue with spreading an array into a function's arguments (above example) is that TypeScript sees the type of You can annotate declare const createSelector: (selectors: number[], fn: Function) => void;
createSelector([1,2,3], () => {});
// type error
createSelector(...[1,2,3], () => {});
const tuple: [number, number, number] = [1, 2, 3];
// type error
createSelector(...tuple, () => {});
createSelector([1,...[2,3]], () => {}) As for spreading into the array, I can't reproduce the problem. Here is the code I tried: // no error
createSelector([selectorFactory('a'), ...selectorFactoryFactory('b', 'c')], (a, b, c) => ({ a, b, c })) |
I'm happy to see this ticket getting some love, thanks everyone! @OliverJAsh You hit the nail on the head with:
Reselect also requires a minimum of two args for the create selector function, so either we'd have to tweak the definitions to allow
I don't think Typing the input as In the below example an error isn't being thrown because the createSelector has a simplified definition via the
|
If variadic types are added to TypeScript, that could be one way of going about this, however it's been under discussion since 2015 so I wouldn't expect it any time soon. |
Microsoft announced TypeScript 3 yesterday which allows for generic typing of the spread operator. We may be able to simplify the Type file for reselect since instead of doing overloads like
We would be able to do something like And this would remove the typescript cap of 13 selectors for reselect. |
Any news on the cap of 13 selectors now that we have TypeScript 3? |
Should be fixed by #486 . |
Hi, I'm trying to create selectorFactoryFactories however am having trouble with the types and was wondering if there's something I'm missing, or if it's a bug, or if it's impossible. Any and all help would be appreciated.
Edit* The destructuring method works perfectly during runtime, the issue is with the type errors being thrown.
The below case works as expected.
However when I destructure part of the array or the complete array, typescript complains.
In the above case typescript errors with
expected 2-13 arguments but got 1 or more
In the above typescript also errors with
The definition for
selectorFactory
andselectorFactoryFactory
andregularSelector
The text was updated successfully, but these errors were encountered: