Skip to content

Commit 8a68c86

Browse files
authored
allow typeof this after for-loops (#46181)
1 parent f424dfc commit 8a68c86

File tree

6 files changed

+170
-2
lines changed

6 files changed

+170
-2
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22496,8 +22496,11 @@ namespace ts {
2249622496
function getFlowCacheKey(node: Node, declaredType: Type, initialType: Type, flowContainer: Node | undefined): string | undefined {
2249722497
switch (node.kind) {
2249822498
case SyntaxKind.Identifier:
22499-
const symbol = getResolvedSymbol(node as Identifier);
22500-
return symbol !== unknownSymbol ? `${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}|${getSymbolId(symbol)}` : undefined;
22499+
if (!isThisInTypeQuery(node)) {
22500+
const symbol = getResolvedSymbol(node as Identifier);
22501+
return symbol !== unknownSymbol ? `${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}|${getSymbolId(symbol)}` : undefined;
22502+
}
22503+
// falls through
2250122504
case SyntaxKind.ThisKeyword:
2250222505
return `0|${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}`;
2250322506
case SyntaxKind.NonNullExpression:

tests/baselines/reference/typeofThis.errors.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,25 @@ tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(57,24):
146146
let y: string = o.this.x; // should narrow to string
147147
}
148148
}
149+
}
150+
151+
class Tests12 {
152+
test1() { // OK
153+
type Test = typeof this;
154+
}
155+
156+
test2() { // OK
157+
for (;;) {}
158+
type Test = typeof this;
159+
}
160+
161+
test3() { // expected no compile errors
162+
for (const dummy in []) {}
163+
type Test = typeof this;
164+
}
165+
166+
test4() { // expected no compile errors
167+
for (const dummy of []) {}
168+
type Test = typeof this;
169+
}
149170
}

tests/baselines/reference/typeofThis.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ class Test11 {
120120
let y: string = o.this.x; // should narrow to string
121121
}
122122
}
123+
}
124+
125+
class Tests12 {
126+
test1() { // OK
127+
type Test = typeof this;
128+
}
129+
130+
test2() { // OK
131+
for (;;) {}
132+
type Test = typeof this;
133+
}
134+
135+
test3() { // expected no compile errors
136+
for (const dummy in []) {}
137+
type Test = typeof this;
138+
}
139+
140+
test4() { // expected no compile errors
141+
for (const dummy of []) {}
142+
type Test = typeof this;
143+
}
123144
}
124145

125146
//// [typeofThis.js]
@@ -241,3 +262,21 @@ var Test11 = /** @class */ (function () {
241262
};
242263
return Test11;
243264
}());
265+
var Tests12 = /** @class */ (function () {
266+
function Tests12() {
267+
}
268+
Tests12.prototype.test1 = function () {
269+
};
270+
Tests12.prototype.test2 = function () {
271+
for (;;) { }
272+
};
273+
Tests12.prototype.test3 = function () {
274+
for (var dummy in []) { }
275+
};
276+
Tests12.prototype.test4 = function () {
277+
for (var _i = 0, _a = []; _i < _a.length; _i++) {
278+
var dummy = _a[_i];
279+
}
280+
};
281+
return Tests12;
282+
}());

tests/baselines/reference/typeofThis.symbols

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,42 @@ class Test11 {
312312
}
313313
}
314314
}
315+
316+
class Tests12 {
317+
>Tests12 : Symbol(Tests12, Decl(typeofThis.ts, 121, 1))
318+
319+
test1() { // OK
320+
>test1 : Symbol(Tests12.test1, Decl(typeofThis.ts, 123, 15))
321+
322+
type Test = typeof this;
323+
>Test : Symbol(Test, Decl(typeofThis.ts, 124, 13))
324+
}
325+
326+
test2() { // OK
327+
>test2 : Symbol(Tests12.test2, Decl(typeofThis.ts, 126, 5))
328+
329+
for (;;) {}
330+
type Test = typeof this;
331+
>Test : Symbol(Test, Decl(typeofThis.ts, 129, 19))
332+
}
333+
334+
test3() { // expected no compile errors
335+
>test3 : Symbol(Tests12.test3, Decl(typeofThis.ts, 131, 5))
336+
337+
for (const dummy in []) {}
338+
>dummy : Symbol(dummy, Decl(typeofThis.ts, 134, 18))
339+
340+
type Test = typeof this;
341+
>Test : Symbol(Test, Decl(typeofThis.ts, 134, 34))
342+
}
343+
344+
test4() { // expected no compile errors
345+
>test4 : Symbol(Tests12.test4, Decl(typeofThis.ts, 136, 5))
346+
347+
for (const dummy of []) {}
348+
>dummy : Symbol(dummy, Decl(typeofThis.ts, 139, 18))
349+
350+
type Test = typeof this;
351+
>Test : Symbol(Test, Decl(typeofThis.ts, 139, 34))
352+
}
353+
}

tests/baselines/reference/typeofThis.types

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,48 @@ class Test11 {
377377
}
378378
}
379379
}
380+
381+
class Tests12 {
382+
>Tests12 : Tests12
383+
384+
test1() { // OK
385+
>test1 : () => void
386+
387+
type Test = typeof this;
388+
>Test : this
389+
>this : any
390+
}
391+
392+
test2() { // OK
393+
>test2 : () => void
394+
395+
for (;;) {}
396+
type Test = typeof this;
397+
>Test : this
398+
>this : any
399+
}
400+
401+
test3() { // expected no compile errors
402+
>test3 : () => void
403+
404+
for (const dummy in []) {}
405+
>dummy : string
406+
>[] : never[]
407+
408+
type Test = typeof this;
409+
>Test : this
410+
>this : any
411+
}
412+
413+
test4() { // expected no compile errors
414+
>test4 : () => void
415+
416+
for (const dummy of []) {}
417+
>dummy : never
418+
>[] : never[]
419+
420+
type Test = typeof this;
421+
>Test : this
422+
>this : any
423+
}
424+
}

tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,25 @@ class Test11 {
122122
let y: string = o.this.x; // should narrow to string
123123
}
124124
}
125+
}
126+
127+
class Tests12 {
128+
test1() { // OK
129+
type Test = typeof this;
130+
}
131+
132+
test2() { // OK
133+
for (;;) {}
134+
type Test = typeof this;
135+
}
136+
137+
test3() { // expected no compile errors
138+
for (const dummy in []) {}
139+
type Test = typeof this;
140+
}
141+
142+
test4() { // expected no compile errors
143+
for (const dummy of []) {}
144+
type Test = typeof this;
145+
}
125146
}

0 commit comments

Comments
 (0)