Skip to content

Less aggressive contextual signature instantiation#33228

Merged
ahejlsberg merged 4 commits intomasterfrom
fix32976
Sep 21, 2019
Merged

Less aggressive contextual signature instantiation#33228
ahejlsberg merged 4 commits intomasterfrom
fix32976

Conversation

@ahejlsberg
Copy link
Copy Markdown
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.

Comment thread src/compiler/checker.ts
getContextualType(node, contextFlags);
const instantiatedType = instantiateContextualType(contextualType, node, contextFlags);
if (instantiatedType) {
if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 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.

Inference of generic constructor function return type.

3 participants