Skip to content

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

Merged
merged 4 commits into from
Sep 21, 2019
Merged

Conversation

ahejlsberg
Copy link
Member

With this PR we perform less aggressive contextual signature instantiation:

declare function foo<T extends (x: string) => string>(f: T): T;

const f = foo(<U>(x: U) => x);  // <U>(x: U) => U

Previously, we'd infer type (x: string) => string for f because we'd obtain the apparent type of T 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.

@@ -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)) {
Copy link
Member

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)?

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inference of generic constructor function return type.
3 participants