Less aggressive contextual signature instantiation#33228
Merged
ahejlsberg merged 4 commits intomasterfrom Sep 21, 2019
Merged
Conversation
weswigham
reviewed
Sep 9, 2019
| getContextualType(node, contextFlags); | ||
| const instantiatedType = instantiateContextualType(contextualType, node, contextFlags); | ||
| if (instantiatedType) { | ||
| if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) { |
Member
There was a problem hiding this comment.
In addition to TypeVariable, should this also be looking for Substitution (since that wraps a type variable pretty directly)?
Member
Author
There was a problem hiding this comment.
Can you write an example that would depend on this?
Member
There was a problem hiding this comment.
declare function foo<T extends (x: string) => string>(f: T): T;
type SubstituteOf<T> = T extends {} ? T : never;
const f = foo(<U>(x: SubstituteOf<U>) => x); // <U>(x: U) => Ustill instantiates as string on this PR.
Member
Author
There was a problem hiding this comment.
No, for your example you get what you'd expect with the PR:
const f = foo(<U>(x: SubstituteOf<U>) => x); // <U>(x: SubstituteOf<U>) => SubstituteOf<U>The substitution type would need to be in the contextual type (i.e. the parameter's type would need to be a substitution type) for the original question you posed to make sense. I don't see how that could happen which is why I asked for an example.
RyanCavanaugh
approved these changes
Sep 16, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this PR we perform less aggressive contextual signature instantiation:
Previously, we'd infer type
(x: string) => stringforfbecause we'd obtain the apparent type ofTand then instantiate the argument type<U>(x: U) => Uin the context of that type. While not incorrect, a better course of action is to leave the argument type alone and just infer it as-is when inferring directly to a type variable. That's the change in this PR.This change also fixes #32976 because we no longer need to check for mixin constructor types (which wasn't really a correct check to begin with).
Fixes #32976.