-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic not inferred inside mapped type parameter #29479
Comments
An interesting thing: type Selector<S, R> = (state: S) => R;
declare function createStructuredSelector<S, T, L extends keyof T = keyof T>(
selectors: {
[K in L]: Selector<S, T[K]>
}
): Selector<S, T>;
type State = { foo: number };
declare const mySelector: Selector<State, boolean>;
// result => Selector<State, {}>
const result = createStructuredSelector({
mySelector
}); |
i have already fixed the case whitch @OliverJAsh has provided, but i have no idea about mine case |
Any updates on this? I'd rather avoid duplication in generics |
One workaround is to use function currying so you only have to specify the first parameter: // I put this in a file called 'reduxUtils.ts'
import * as Reselect from "reselect";
export function createStructuredSelector<S>() {
return function<T>(
selectors: { [K in keyof T]: Reselect.Selector<S, T[K]> }
) {
return Reselect.createStructuredSelector<S, T>(selectors);
};
} Usage: const result = createStructuredSelector<State>()({ mySelector }); |
now it's |
The interesting thing is that if we add more parameters to type Selector<S, R> = (state: S, meta: number) => R;
declare function createStructuredSelector<S, T>(selectors: {
[K in keyof T]: Selector<S, T[K]>;
}): Selector<S, T>;
type State = { foo: number };
declare const mySelector: Selector<State, boolean>
const result1 = createStructuredSelector({
// Type 'Selector<State, boolean>' is not assignable to type 'Selector<unknown, boolean>'.
// Type 'unknown' is not assignable to type 'State'.(2322)
mySelector: mySelector,
});
const result2 = createStructuredSelector({
// ok
mySelector: (state: State) => true,
});
result2;
// ^? const result: Selector<State, { mySelector: boolean; }>
export {}; If we just remove |
TypeScript Version: 3.2.2
Search Terms: generic inference {} createStructuredSelector mapped types
Code
The text was updated successfully, but these errors were encountered: