Type inference issues caused by default param values or any/unknown type (v4.1.5)
I encountered two issues with type inference in v4.1.5:
-
When an input-selector expects more than one param and the last param is typed as unknown/any, TS complains that the final selector expect 1 param instead of 2.
-
When a default value is provided to any param of at least one input-selector without providing a type explicitly, all params to an output-selector are implicitly typed as any.
This code sandbox shows the issue: https://codesandbox.io/s/reselect-ts-issues-opbhs?file=/src/index.ts:272-279
Hmm. Afraid I don't have time to investigate this atm, but my gut says there may not be much we can do about either of these.
We get the types of the function arguments using the TS Parameters built-in type. If it isn't including those arguments in the result, I'm not sure there's any way for us to adjust for that.
It seems to be a general issue that if the infer fails for the parameters, it falls back to assume there is no argument and hides any error e.g. the 2nd issue, if I change it to this
const getDefaultKey = (state: State) => state.defaultKey;
const selector2 = createSelector(
(state: State, key: string) => "key",
(state: State, key2: number) => key2,
(state: State, key3: string) => key3,
(key, key2, key3) => key + key2 + key3
);
selector2({} as State); // No error
selector2({} as State, 2); // error, but unexpected
as a consumer of the selector I am being told by typescript to not give a 2nd argument and if I don't I get no error. but my selectors actually require a 2nd argument but are mistyped, maybe one is a number and should have been a string.
The problem is that
/** Determines the combined "Params" type (all remaining args) from all input selectors */
export type GetParamsFromSelectors<
S extends SelectorArray,
RemainingItems extends readonly unknown[] = Tail<MergeParameters<S>>
// This seems to default to an array containing an empty object, which is
// not meaningful and causes problems with the `Selector/OutputSelector` types.
// Force it to have a meaningful value, or cancel it out.
> = RemainingItems extends [EmptyObject] ? never : RemainingItems
RemainingItems can be [never] and it seems that [never] ends up adding onto a params array and just becoming an empty array.
We can probably escape out to never if remaining items is [never] though to support multi arguments we would need a way to determine if the array can contain never
I am having a go at a fix :S
I made a PR - https://github.com/reduxjs/reselect/pull/564
Its not a ready-to-go fix but I hope it helps.
Alternately if you can help me with ideas on what to do I can take another crack at it.
Issue 1 cause is this
export type GetParamsFromSelectors<
S extends SelectorArray,
RemainingItems extends readonly unknown[] = Tail<MergeParameters<S>>
// This seems to default to an array containing an empty object, which is
// not meaningful and causes problems with the `Selector/OutputSelector` types.
// Force it to have a meaningful value, or cancel it out.
> = RemainingItems extends [EmptyObject] ? never : RemainingItems
if RemainingItems is [any] or [unknown] it extends [EmptyObject]
I don't understand the comment because I haven't seen it default to an array with an empty object. When I remove the condition breaking things, no tests fail.. so I am not sure but maybe this was a legacy problem solved elsewhere? I will add it into my PR.
Hmm. I know I added that intentionally during the 4.1.x process, but it's also possible it's no longer needed at this point.
Sorry, forgot to mention this should be fixed in https://github.com/reduxjs/reselect/releases/tag/v4.1.6 .