Skip to content

Inferred type parameter constraints #21709

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

Merged
merged 9 commits into from
Feb 7, 2018

Conversation

ahejlsberg
Copy link
Member

With this PR we infer constraints for infer X type parameters in conditional types in certain circumstances. Specifically, when an infer X type parameter declaration occurs as a type argument in a type reference, we infer the constraint of the corresponding type parameter as the constraint of X. For example:

type Foo<T extends string> = { foo: T };
type Bar<T extends string> = { bar: T };
type Barify<T> = T extends Foo<infer X> ? Bar<X> : T;

Here, because the infer X declaration occurs as a type argument for the T type parameter in Foo, and because T has a constraint of string, the inferred constraint for X is string. Therefore, X meets the constraint required by Bar<T>.

Fixes #21631.

@ahejlsberg ahejlsberg merged commit 3d99812 into master Feb 7, 2018
@ahejlsberg ahejlsberg deleted the inferredTypeParameterConstraints branch February 7, 2018 21:16
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants