-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Less aggressive contextual signature instantiation #33228
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
Conversation
@@ -19193,7 +19194,7 @@ namespace ts { | |||
getContextualTypeForObjectLiteralMethod(node, contextFlags) : | |||
getContextualType(node, contextFlags); | |||
const instantiatedType = instantiateContextualType(contextualType, node, contextFlags); | |||
if (instantiatedType) { | |||
if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to TypeVariable
, should this also be looking for Substitution
(since that wraps a type variable pretty directly)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write an example that would depend on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) => U
still instantiates as string
on this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
With this PR we perform less aggressive contextual signature instantiation:
Previously, we'd infer type
(x: string) => string
forf
because we'd obtain the apparent type ofT
and then instantiate the argument type<U>(x: U) => U
in 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.