Skip to content

Commit 0f483d6

Browse files
committed
Assign and instantiate contextual this type if not present
1 parent 271ffc8 commit 0f483d6

9 files changed

+72
-67
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,9 +4785,6 @@ namespace ts {
47854785

47864786
function getThisTypeOfSignature(signature: Signature): Type | undefined {
47874787
if (signature.thisParameter) {
4788-
if (signature.mapper) {
4789-
signature = instantiateSignature(signature, signature.mapper);
4790-
}
47914788
return getTypeOfSymbol(signature.thisParameter);
47924789
}
47934790
}
@@ -9085,15 +9082,15 @@ namespace ts {
90859082
return getInferredClassType(classSymbol);
90869083
}
90879084
}
9088-
const type = getContextuallyTypedThisType(container);
9089-
if (type) {
9090-
return type;
9091-
}
90929085

90939086
const thisType = getThisTypeOfDeclaration(container);
90949087
if (thisType) {
90959088
return thisType;
90969089
}
9090+
const type = getContextuallyTypedThisType(container);
9091+
if (type) {
9092+
return type;
9093+
}
90979094
}
90989095
if (isClassLike(container.parent)) {
90999096
const symbol = getSymbolOfNode(container.parent);
@@ -12277,8 +12274,10 @@ namespace ts {
1227712274
function assignContextualParameterTypes(signature: Signature, context: Signature, mapper: TypeMapper) {
1227812275
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
1227912276
if (context.thisParameter) {
12280-
// save the mapper in case we need to type `this` later
12281-
context.mapper = mapper;
12277+
if (!signature.thisParameter) {
12278+
signature.thisParameter = createTransientSymbol(context.thisParameter, undefined);
12279+
}
12280+
assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper);
1228212281
}
1228312282
for (let i = 0; i < len; i++) {
1228412283
const parameter = signature.parameters[i];

tests/baselines/reference/instantiateContextuallyTypedGenericThis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
interface JQuery {
33
each<T>(
44
collection: T[], callback: (this: T, dit: T) => T
5-
): any;
5+
): T[];
66
}
77

88
let $: JQuery;

tests/baselines/reference/instantiateContextuallyTypedGenericThis.symbols

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ interface JQuery {
1616
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
1717
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
1818

19-
): any;
19+
): T[];
20+
>T : Symbol(T, Decl(instantiateContextuallyTypedGenericThis.ts, 1, 9))
2021
}
2122

2223
let $: JQuery;
@@ -38,6 +39,7 @@ $.each(lines, function(dit) {
3839
>dit : Symbol(dit, Decl(instantiateContextuallyTypedGenericThis.ts, 8, 23))
3940
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
4041
>this.charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
42+
>this : Symbol(this, Decl(instantiateContextuallyTypedGenericThis.ts, 2, 36))
4143
>charAt : Symbol(String.charAt, Decl(lib.d.ts, --, --))
4244

4345
});

tests/baselines/reference/instantiateContextuallyTypedGenericThis.types

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ interface JQuery {
33
>JQuery : JQuery
44

55
each<T>(
6-
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => any
6+
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
77
>T : T
88

99
collection: T[], callback: (this: T, dit: T) => T
@@ -16,7 +16,8 @@ interface JQuery {
1616
>T : T
1717
>T : T
1818

19-
): any;
19+
): T[];
20+
>T : T
2021
}
2122

2223
let $: JQuery;
@@ -27,12 +28,12 @@ let lines: string[];
2728
>lines : string[]
2829

2930
$.each(lines, function(dit) {
30-
>$.each(lines, function(dit) { return dit.charAt(0) + this.charAt(1);}) : any
31-
>$.each : <T>(collection: T[], callback: (this: T, dit: T) => T) => any
31+
>$.each(lines, function(dit) { return dit.charAt(0) + this.charAt(1);}) : string[]
32+
>$.each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
3233
>$ : JQuery
33-
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => any
34+
>each : <T>(collection: T[], callback: (this: T, dit: T) => T) => T[]
3435
>lines : string[]
35-
>function(dit) { return dit.charAt(0) + this.charAt(1);} : (dit: string) => string
36+
>function(dit) { return dit.charAt(0) + this.charAt(1);} : (this: string, dit: string) => string
3637
>dit : string
3738

3839
return dit.charAt(0) + this.charAt(1);

tests/baselines/reference/thisTypeInFunctions.symbols

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let impl: I = {
135135

136136
return this.a;
137137
>this.a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
138-
>this : Symbol(, Decl(thisTypeInFunctions.ts, 24, 28))
138+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 24, 23))
139139
>a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
140140

141141
},
@@ -144,7 +144,7 @@ let impl: I = {
144144

145145
return this.a;
146146
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
147-
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
147+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 25, 22))
148148
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
149149

150150
},
@@ -153,7 +153,7 @@ let impl: I = {
153153

154154
return this.a;
155155
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
156-
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
156+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 26, 17))
157157
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
158158

159159
},
@@ -173,15 +173,15 @@ impl.explicitStructural = function() { return this.a; };
173173
>impl : Symbol(impl, Decl(thisTypeInFunctions.ts, 37, 3))
174174
>explicitStructural : Symbol(I.explicitStructural, Decl(thisTypeInFunctions.ts, 23, 38))
175175
>this.a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
176-
>this : Symbol(, Decl(thisTypeInFunctions.ts, 24, 28))
176+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 24, 23))
177177
>a : Symbol(a, Decl(thisTypeInFunctions.ts, 24, 30))
178178

179179
impl.explicitInterface = function() { return this.a; };
180180
>impl.explicitInterface : Symbol(I.explicitInterface, Decl(thisTypeInFunctions.ts, 24, 50))
181181
>impl : Symbol(impl, Decl(thisTypeInFunctions.ts, 37, 3))
182182
>explicitInterface : Symbol(I.explicitInterface, Decl(thisTypeInFunctions.ts, 24, 50))
183183
>this.a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
184-
>this : Symbol(I, Decl(thisTypeInFunctions.ts, 19, 21))
184+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 25, 22))
185185
>a : Symbol(I.a, Decl(thisTypeInFunctions.ts, 20, 13))
186186

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

205205
// parameter checking
@@ -536,7 +536,7 @@ c.explicitC = function(m) { return this.n + m };
536536
>explicitC : Symbol(C.explicitC, Decl(thisTypeInFunctions.ts, 8, 5))
537537
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 126, 23))
538538
>this.n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
539-
>this : Symbol(C, Decl(thisTypeInFunctions.ts, 3, 1))
539+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 9, 14))
540540
>n : Symbol(C.n, Decl(thisTypeInFunctions.ts, 4, 9))
541541
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 126, 23))
542542

@@ -546,7 +546,7 @@ c.explicitProperty = function(m) { return this.n + m };
546546
>explicitProperty : Symbol(C.explicitProperty, Decl(thisTypeInFunctions.ts, 11, 5))
547547
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 127, 30))
548548
>this.n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28))
549-
>this : Symbol(, Decl(thisTypeInFunctions.ts, 12, 26))
549+
>this : Symbol(this, Decl(thisTypeInFunctions.ts, 12, 21))
550550
>n : Symbol(n, Decl(thisTypeInFunctions.ts, 12, 28))
551551
>m : Symbol(m, Decl(thisTypeInFunctions.ts, 127, 30))
552552

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

0 commit comments

Comments
 (0)