-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Why I use function generic parameters as the other type's generic has compile error? #29225
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
Comments
Reduced example: function example<Super, Sub extends Super>() {
const shouldWork: Sub extends Super ? number : string = 78787; // error: does not simplif
}
It seems that the conditional type gets stuck, even though it should simplify since we know that |
I think the |
I wasn’t even aware this distinction existed; I’m kind of curious now, what’s the difference? (Preferably with examples) |
@jack-williams @fatcerberus I have a another piece.I think is a similar question. export interface Action<T = any> {
type: T;
}
export interface AnyAction extends Action {
[extraProps: string]: any;
}
export type Reducer<S = any, A extends Action = AnyAction> = (
state: S | undefined,
action: A
) => S;
export type ReducersMapObject<S extends any= any, A extends Action = Action> = {
[K in keyof S]: Reducer<S[K], A>
};
function assertShape<S extends any, A extends Action>(
reducers: ReducersMapObject<S, A>
) {
let reducerKeys = Object.keys(reducers);
reducerKeys.forEach(key => {
let reducer = reducers[key];
const SET_AGE="set age haha"
const action={
type:SET_AGE
}
// Argument of type '{ type: string; }' is not assignable to parameter of type 'A'.
reducer({sf:"rewr"}, action);
});
} |
@linmodev That looks a different situation to me, which I think is ok. The type of const a: (state: "a" | undefined, action: Action<number>) => "a" = () => "a";
assertShape<{ a: "a" }, Action<number>>({ a }); @fatcerberus // Return trueType for a definitely true extends check. The definitely assignable relation excludes
// type variable constraints from consideration. Without the definitely assignable relation, the type
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
// would immediately resolve to 'string' instead of being deferred. |
@jack-williams But the compile error just show the second parameter of reducer's type is type A.How to resolve this? |
@linmodev I'm not sure what your use-case is, so I don't know how to fix the error. I believe the error message is correct; in the body of the function, the type
|
@jack-williams Thanks ,Have read the issue ,I find the key point of this eventually. I will continually pay a attention on it |
This is a design limitation. The algorithm we use to determine whether to defer resolution of a conditional type doesn't consider constraints of type variables. Specifically, for a conditional type
We use these rules because type parameters that appear unrelated, such as |
TypeScript Version: 3.3.0-dev.20190101
Search Terms: Why I use function generic parameters as the other type's generic has compile error?
Code
Expected behavior:
Actual behavior:
Playground Link: Link
The text was updated successfully, but these errors were encountered: