-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Conditional type is deferred because narrow-as-assertion on constrained type parameter has no effect #29939
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
I understand why type parameter constraints are ignored by the restrictive instantiation. Using transitive reasoning with type Foo<T extends { x: any }> = T extends { x: string } ? string : number A question I have is: are there other examples that do not involve type Bar<T extends Object> = T extends object ? string : number Would it be fair to say that the restrictive instantiation is currently necessary purely because conditional type resolution uses assignability, a non-transitive relation? I know that |
I think I know enough about this now to say that it's a design limitation.
This leads me to the conclusion that this is just an awkward composition of concepts, but one that few people seem to face. |
@jack-williams Would be really great to learn something here. Currently I'm trying to use a conditional type as return type of a function. Inside the function I'm narrowing down on the constraints of the conditional type to return the corresponding value (similar to this SO post. As I read your posts here I'm wondering about the square bracket notation in your And you wrote considering constraints in conditional types would sometimes be incorrect. I assume this is the reason why writing a function with conditional return type is not possible. Right? And could you explain why? |
Sorry for the gratuitous title.
Code
Expected behavior:
The two to behave the same; probably, they should both be OK.
Actual behavior:
The type of
x
does not get narrowed by assertion, presumably because the assertion adds no information. Conditional type resolution ignores parameter constraints when attempting to resolve using the most restrictive instantiation, and thereforeIsString<typeof x>
does not resolve.Essentially
T extends string
, andT & string
whereT
is unconstrained, do not behave the same in conditional check types. The former has its constraint ignored, while the latter gets to use its intersection proof.Playground Link: link
Related Issues:
The text was updated successfully, but these errors were encountered: