-
Notifications
You must be signed in to change notification settings - Fork 671
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
TypeScript errors with --strictFunctionTypes flag. #391
Comments
I also just ran into this. Filed a TypeScript issue. See the issue for details on workaround: microsoft/TypeScript#29479. |
Is this still an issue with TS 4.x? |
Yes. Just tested this example with TS 4.1.2 and reselect 4.0.0 |
Okay. Do the changes in #480 do anything to improve the situation? |
No. Although the error message has changed.
|
So it looks like the original typing of You basically want this: import { createStructuredSelector } from 'reselect';
type GlobalState = {
foo: string;
bar: number;
};
const selectFoo = (state: GlobalState) => state.foo;
const selectBar = (state: GlobalState) => state.bar;
// <Input, Output>
const injectState = createStructuredSelector<GlobalState, { foo: string, bar: number }>({
foo: selectFoo,
bar: selectBar,
}); where Basically for this typing to work, you need to provide the "input" and "output" types. @markerikson we should note this as something we might be able to improve in #484. Not sure how easy it will be though. |
There is a minor improvement we can do to make this easier in 4.x which is to provide a default for the "output" type, so if people just want to "map" a state over, they can do that. Granted, I'm not wholly convinced this adds much to the experience. @markerikson I'll add a test for this in #486 like this: function testStructuredSelectorTypeParams() {
type GlobalState = {
foo: string;
bar: number;
};
const selectFoo = (state: GlobalState) => state.foo;
const selectBar = (state: GlobalState) => state.bar;
// Output state should be the same as input, if not provided
// @ts-expect-error
createStructuredSelector<GlobalState>({
foo: selectFoo,
// bar: selectBar,
// ^^^ because this is missing, an error is thrown
});
// This works
createStructuredSelector<GlobalState>({
foo: selectFoo,
bar: selectBar,
});
// So does this
createStructuredSelector<GlobalState, Omit<GlobalState, 'bar'>>({
foo: selectFoo,
});
} It's easy enough to revert if you think it's overkill. |
Should be fixed by #486 . |
reselect: 4.0.0
typescript: 3.2.2
The following code produces TS error.
Error text
With
strictFunctionTypes
disabled, everything works fine. What is the correct way of usingcreateStructuredSelector
in TypeScript withstrictFunctionTypes
option?The text was updated successfully, but these errors were encountered: