Skip to content

Instantiate this for contextually typed type parameters #9746

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3063,9 +3063,14 @@ namespace ts {
}
}
// Use contextual parameter type if one is available
const type = declaration.symbol.name === "this"
? getContextuallyTypedThisType(func)
: getContextuallyTypedParameterType(<ParameterDeclaration>declaration);
let type: Type;
if (declaration.symbol.name === "this") {
const thisParameter = getContextualThisParameter(func);
type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined;
}
else {
type = getContextuallyTypedParameterType(<ParameterDeclaration>declaration);
}
if (type) {
return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality);
}
Expand Down Expand Up @@ -4689,6 +4694,9 @@ namespace ts {
if (isJSConstructSignature) {
minArgumentCount--;
}
if (!thisParameter && isObjectLiteralMethod(declaration)) {
thisParameter = getContextualThisParameter(declaration);
}

const classType = declaration.kind === SyntaxKind.Constructor ?
getDeclaredTypeOfClassOrInterface(getMergedSymbol((<ClassDeclaration>declaration.parent).symbol))
Expand Down Expand Up @@ -9082,10 +9090,6 @@ namespace ts {
return getInferredClassType(classSymbol);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to save this block for contextually-typed object literals like this:

interface CI {
  m: number
  method(this: this, n: number);
}
let o: CI = {
  m: 12,
  method(n) {
    return this.m + n;
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our in-person discussion, I checked on contextual typing for parameters. It's 1 of 9 cases in getTypeForVariableLikeDeclaration:

                const type = declaration.symbol.name === "this"
                    ? getContextuallyTypedThisType(func)
                    : getContextuallyTypedParameterType(<ParameterDeclaration>declaration);

The contextual typing for an undeclared this is basically the same thing here in checkThisExpression since there is no parameter to get the type of.

}
}
const type = getContextuallyTypedThisType(container);
if (type) {
return type;
}

const thisType = getThisTypeOfDeclaration(container);
if (thisType) {
Expand Down Expand Up @@ -9326,11 +9330,11 @@ namespace ts {
}
}

function getContextuallyTypedThisType(func: FunctionLikeDeclaration): Type {
function getContextualThisParameter(func: FunctionLikeDeclaration): Symbol {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better name would be getContextualThisParameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== SyntaxKind.ArrowFunction) {
const contextualSignature = getContextualSignature(func);
if (contextualSignature) {
return getThisTypeOfSignature(contextualSignature);
return contextualSignature.thisParameter;
}
}

Expand Down Expand Up @@ -12273,6 +12277,12 @@ namespace ts {

function assignContextualParameterTypes(signature: Signature, context: Signature, mapper: TypeMapper) {
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
if (context.thisParameter) {
if (!signature.thisParameter) {
signature.thisParameter = createTransientSymbol(context.thisParameter, undefined);
}
assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper);
}
for (let i = 0; i < len; i++) {
const parameter = signature.parameters[i];
const contextualParameterType = getTypeAtPosition(context, i);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [instantiateContextuallyTypedGenericThis.ts]
interface JQuery {
each<T>(
collection: T[], callback: (this: T, dit: T) => T
): T[];
}

let $: JQuery;
let lines: string[];
$.each(lines, function(dit) {
return dit.charAt(0) + this.charAt(1);
});


//// [instantiateContextuallyTypedGenericThis.js]
var $;
var lines;
$.each(lines, function (dit) {
return dit.charAt(0) + this.charAt(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
=== tests/cases/compiler/instantiateContextuallyTypedGenericThis.ts ===
interface JQuery {
>JQuery : Symbol(JQuery, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 0))

each<T>(
>each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18))
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))

collection: T[], callback: (this: T, dit: T) => T
>collection : Symbol(collection, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 12))
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
>callback : Symbol(callback, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 24))
>this : Symbol(this, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 36))
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 44))
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))

): T[];
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
}

let $: JQuery;
>$ : Symbol($, Decl(instantiateContextuallyTypedGenericThis.ts, 6, 3))
>JQuery : Symbol(JQuery, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 0))

let lines: string[];
>lines : Symbol(lines, Decl(instantiateContextuallyTypedGenericThis.ts, 7, 3))

$.each(lines, function(dit) {
>$.each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18))
>$ : Symbol($, Decl(instantiateContextuallyTypedGenericThis.ts, 6, 3))
>each : Symbol(JQuery.each, Decl(instantiateContextuallyTypedGenericThis.ts, 0, 18))
>lines : Symbol(lines, Decl(instantiateContextuallyTypedGenericThis.ts, 7, 3))
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 8, 23))

return dit.charAt(0) + this.charAt(1);
>dit.charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 8, 23))
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
>this.charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
>this : Symbol(this, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 36))
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/instantiateContextuallyTypedGenericThis.ts ===
interface JQuery {
>JQuery : JQuery

each<T>(
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
>T : T

collection: T[], callback: (this: T, dit: T) => T
>collection : T[]
>T : T
>callback : (this: T, dit: T) => T
>this : T
>T : T
>dit : T
>T : T
>T : T

): T[];
>T : T
}

let $: JQuery;
>$ : JQuery
>JQuery : JQuery

let lines: string[];
>lines : string[]

$.each(lines, function(dit) {
>$.each(lines, function(dit) { return dit.charAt(0) + this.charAt(1);}) : string[]
>$.each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
>$ : JQuery
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
>lines : string[]
>function(dit) { return dit.charAt(0) + this.charAt(1);} : (this: string, dit: string) => string
>dit : string

return dit.charAt(0) + this.charAt(1);
>dit.charAt(0) + this.charAt(1) : string
>dit.charAt(0) : string
>dit.charAt : (pos: number) => string
>dit : string
>charAt : (pos: number) => string
>0 : number
>this.charAt(1) : string
>this.charAt : (pos: number) => string
>this : string
>charAt : (pos: number) => string
>1 : number

});

18 changes: 9 additions & 9 deletions tests/baselines/reference/thisTypeInFunctions.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ let impl: I = {

return this.a;
>this.a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
>this : Symbol(, Decl(thisTypeInFunctions.ts, 24, 28))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 24, 23))
>a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))

},
Expand All @@ -144,7 +144,7 @@ let impl: I = {

return this.a;
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 25, 22))
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))

},
Expand All @@ -153,7 +153,7 @@ let impl: I = {

return this.a;
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 26, 17))
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))

},
Expand All @@ -173,15 +173,15 @@ impl.explicitStructural = function() { return this.a; };
>impl : Symbol(impl, Decl(thisTypeInFunctions.ts, 37, 3))
>explicitStructural : Symbol(I.explicitStructural, Decl(thisTypeInFunctions.ts, 23, 38))
>this.a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
>this : Symbol(, Decl(thisTypeInFunctions.ts, 24, 28))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 24, 23))
>a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))

impl.explicitInterface = function() { return this.a; };
>impl.explicitInterface : Symbol(I.explicitInterface, Decl(thisTypeInFunctions.ts, 24, 50))
>impl : Symbol(impl, Decl(thisTypeInFunctions.ts, 37, 3))
>explicitInterface : Symbol(I.explicitInterface, Decl(thisTypeInFunctions.ts, 24, 50))
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 25, 22))
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))

impl.explicitStructural = () => 12;
Expand All @@ -199,7 +199,7 @@ impl.explicitThis = function () { return this.a; };
>impl : Symbol(impl, Decl(thisTypeInFunctions.ts, 37, 3))
>explicitThis : Symbol(I.explicitThis, Decl(thisTypeInFunctions.ts, 25, 39))
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 26, 17))
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))

// parameter checking
Expand Down Expand Up @@ -536,7 +536,7 @@ c.explicitC = function(m) { return this.n + m };
>explicitC : Symbol(C.explicitC, Decl(thisTypeInFunctions.ts, 8, 5))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 126, 23))
>this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
>this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 9, 14))
>n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 126, 23))

Expand All @@ -546,7 +546,7 @@ c.explicitProperty = function(m) { return this.n + m };
>explicitProperty : Symbol(C.explicitProperty, Decl(thisTypeInFunctions.ts, 11, 5))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 127, 30))
>this.n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28))
>this : Symbol(, Decl(thisTypeInFunctions.ts, 12, 26))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 12, 21))
>n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 127, 30))

Expand All @@ -556,7 +556,7 @@ c.explicitThis = function(m) { return this.n + m };
>explicitThis : Symbol(C.explicitThis, Decl(thisTypeInFunctions.ts, 5, 14))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 128, 26))
>this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
>this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1))
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 6, 17))
>n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 128, 26))

Expand Down
Loading