Skip to content
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

Conditional Types: Type parameters with unknown constraint are not the most restrictive type when relating from any. #29674

Closed
jack-williams opened this issue Feb 1, 2019 · 2 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@jack-williams
Copy link
Collaborator

jack-williams commented Feb 1, 2019

TypeScript Version: master

A conditional type immediately resolves to the true branch when the condition is satisfied with type parameters instantiated at the most restrictive type---a type parameter with unknown constraint.

A type parameter with unknown constraint is not the most restrictive type in all cases: any may be assigned to any type parameter, but any may not be assigned to never.

Code `

type Contrived<T> = [any] extends [T] ? true : false; // true
type ContrivedConcrete = [any] extends [never] ? true : false; // false

Related Issues: These PR's #29338 #29437

To be honest, this issue is incredibly niche, and almost exclusively a result of any (that I currently know of). The behaviour is also consistent with how things work in other cases, such as:

function foo<T>(x: any): T {
    return x;
}
const a = foo<never>(3);

While the conditional behaviour is consistent with other cases where a type parameter can later be instantiated to never, I'm not sure anyone would ever want that behaviour in a conditional. Regardless of whether this is by design, a limitation, or a bug---I think it would be good to have this documented somewhere in the tracker.

@jack-williams jack-williams changed the title Conditional Types: Type parameters with unknown constraint are not the most restrictive type when relating to any. Conditional Types: Type parameters with unknown constraint are not the most restrictive type when relating from any. Feb 1, 2019
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 5, 2019
@jack-williams
Copy link
Collaborator Author

FWIW: I'm not actually sure what the right thing should be here; I can see arguments for both approaches. I guess all I would like to get out of the issue is some explanation I can link to if someone ever stumbles upon this.

@jack-williams
Copy link
Collaborator Author

Closing for house-keeping---seems unactionable right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants