-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Function parameter type inference error #59949
Comments
Because checking what value This is a perfectly valid call: |
How should I constrain the type of "extend" ? Do you have any suggestions ? Thank you so much ! |
Duplicate #33014 For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis: function func<T extends Type>(type: T, extend: X<T>) {
const x: { [K in Type]: (type: K, x: X<K>) => void } = {
[Type.A](type, extend) {
console.log(type, extend);
// ^? (parameter) extend: string
},
[Type.B](type, extend) {
}
}
x[type](type, extend);
}
func(Type.A, '');
func(Type.B, 1);
func(Math.random() < 0.99 ? Type.A : Type.B, 1); // still allowed, #47109 technically unsound this way or, you need to use control flow analysis on a discriminated union and not use generics: function func(...[type, extend]: { [T in Type]: [type: T, extend: X<T>] }[Type]) {
if (type === Type.A) {
console.log(type, extend);
// ^? (parameter) extend: string
}
}
func(Type.A, '');
func(Type.B, 1);
func(Math.random() < 0.99 ? Type.A : Type.B, 1); // error |
Good job |
🔎 Search Terms
Function parameter
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/zh/play/?#code/KYOwrgtgBAKgngB2FA3gWAFBSgQQDSbYBCBGAvppgJYgAuwATgGYCGAxsvEgLIsKqEoAbS7AAdDgC6ALigBnWgxoBzANyCRicURlRwEAEaN15ShlpaoADQA8MKMAAe9EABM5sLQD4oAXmyivAgikiaYTGAgbLRUAPYgUBFRdg7OoO6eSF4AFBZIsjB4qS6usrYwXgCUAlhQVExQuZa+LZniONXotdgA9D1QAOoAFnBQAEROJWNQechUHiCxtPKKKlAA-IK9-dkILAwsEMD0DNWT6bIKSiDKUAA+epBGDPdQBrGxADbALCBbUGx4nIvuJPrFlE0kEVzm5KiZsBRTBhwpE2NlRBIigByLFwlFRdFaMQkKAARjhQA
💻 Code
🙁 Actual behavior
In the example, Why "extend" type is not string ?
🙂 Expected behavior
The "type" constrained to "Type.A",so "extend" shuld be "string".
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: