-
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
Cannot widen type in generics while retaining knowledge of assignability #35257
Comments
If this is "working as intended" or my expectations are otherwise infeasible, is there any other valid way to achieve what I am attempting? For example, I want |
Assignability for unresolved generic conditional types is an existing sore point. (Does anyone know if there is a canonical issue for this? This has to be a duplicate or related to something, but I'm not sure. It's not exactly #26933) In cases like this I usually hit the compiler over the head with assignability using an intersection: public isValidValue(value: Widen<T>): value is Widen<T> & T {
return this.validValues.indexOf(value as any) !== -1;
} |
This is really only possible to know through second-order reasoning about all possible |
@RyanCavanaugh Thanks for the explanation. I see how that would be quite complex. Do you have any ideas for:
|
Search Terms:
generics widen type
Code
Expected behavior:
The
Widen
type in my example widens number and string types. For any typeT
,T
is assignable toWiden<T>
. I expect to be able to rely on this fact in generics.Actual behavior:
The assignability relationship of
T
andWiden<T>
is lost whenT
is a generic type rather than a concrete type (see// UNEXPECTED ERROR
comment in example).Playground Link:
http://www.typescriptlang.org/play/?ssl=5&ssc=7&pln=5&pc=12#code/C4TwDgpgBA6glgEwgOwDwBUoQB7BQgZymQFcBbAIwgCcoAfKA4auZAcwD4oBeKTHPMkLFyVagCgoUqAH4RlGpOkAuPllz4iTFuyVS521mz1RV6ANzjxAYwA2AQwJEAYgHtXGdYOGkFtBobsXADeSmAkFLZw1lDWrsjaJNbArtQAFGAsAG72eFDUEPYI8bYgUDlRCABq9rYkEASqBUUlZegA2gC6AJRQwVAAvlZSAPQjUACqAHIAogAaAAozAMLoMwAiUDMAStsA8ttmUHBEyK7AUI4EcGzI9pHQKbCIKBgcYRFRMSc1lb-1aQq9VU8CQaHQHG6qiB0BOalC0nyEGAJGoyCgwAAFicAHQVRD-Bo41hIbB7ABmgNq9UuRHsyBAvQAhNxeABaACMlikQwGQA
The text was updated successfully, but these errors were encountered: