allow typeof this after for-loops (#46181)

This commit is contained in:
Zzzen 2021-10-28 01:26:05 +08:00 committed by GitHub
parent f424dfc18a
commit 8a68c8616d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 170 additions and 2 deletions

View file

@ -22496,8 +22496,11 @@ namespace ts {
function getFlowCacheKey(node: Node, declaredType: Type, initialType: Type, flowContainer: Node | undefined): string | undefined {
switch (node.kind) {
case SyntaxKind.Identifier:
const symbol = getResolvedSymbol(node as Identifier);
return symbol !== unknownSymbol ? `${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}|${getSymbolId(symbol)}` : undefined;
if (!isThisInTypeQuery(node)) {
const symbol = getResolvedSymbol(node as Identifier);
return symbol !== unknownSymbol ? `${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}|${getSymbolId(symbol)}` : undefined;
}
// falls through
case SyntaxKind.ThisKeyword:
return `0|${flowContainer ? getNodeId(flowContainer) : "-1"}|${getTypeId(declaredType)}|${getTypeId(initialType)}`;
case SyntaxKind.NonNullExpression:

View file

@ -146,4 +146,25 @@ tests/cases/conformance/types/specifyingTypes/typeQueries/typeofThis.ts(57,24):
let y: string = o.this.x; // should narrow to string
}
}
}
class Tests12 {
test1() { // OK
type Test = typeof this;
}
test2() { // OK
for (;;) {}
type Test = typeof this;
}
test3() { // expected no compile errors
for (const dummy in []) {}
type Test = typeof this;
}
test4() { // expected no compile errors
for (const dummy of []) {}
type Test = typeof this;
}
}

View file

@ -120,6 +120,27 @@ class Test11 {
let y: string = o.this.x; // should narrow to string
}
}
}
class Tests12 {
test1() { // OK
type Test = typeof this;
}
test2() { // OK
for (;;) {}
type Test = typeof this;
}
test3() { // expected no compile errors
for (const dummy in []) {}
type Test = typeof this;
}
test4() { // expected no compile errors
for (const dummy of []) {}
type Test = typeof this;
}
}
//// [typeofThis.js]
@ -241,3 +262,21 @@ var Test11 = /** @class */ (function () {
};
return Test11;
}());
var Tests12 = /** @class */ (function () {
function Tests12() {
}
Tests12.prototype.test1 = function () {
};
Tests12.prototype.test2 = function () {
for (;;) { }
};
Tests12.prototype.test3 = function () {
for (var dummy in []) { }
};
Tests12.prototype.test4 = function () {
for (var _i = 0, _a = []; _i < _a.length; _i++) {
var dummy = _a[_i];
}
};
return Tests12;
}());

View file

@ -312,3 +312,42 @@ class Test11 {
}
}
}
class Tests12 {
>Tests12 : Symbol(Tests12, Decl(typeofThis.ts, 121, 1))
test1() { // OK
>test1 : Symbol(Tests12.test1, Decl(typeofThis.ts, 123, 15))
type Test = typeof this;
>Test : Symbol(Test, Decl(typeofThis.ts, 124, 13))
}
test2() { // OK
>test2 : Symbol(Tests12.test2, Decl(typeofThis.ts, 126, 5))
for (;;) {}
type Test = typeof this;
>Test : Symbol(Test, Decl(typeofThis.ts, 129, 19))
}
test3() { // expected no compile errors
>test3 : Symbol(Tests12.test3, Decl(typeofThis.ts, 131, 5))
for (const dummy in []) {}
>dummy : Symbol(dummy, Decl(typeofThis.ts, 134, 18))
type Test = typeof this;
>Test : Symbol(Test, Decl(typeofThis.ts, 134, 34))
}
test4() { // expected no compile errors
>test4 : Symbol(Tests12.test4, Decl(typeofThis.ts, 136, 5))
for (const dummy of []) {}
>dummy : Symbol(dummy, Decl(typeofThis.ts, 139, 18))
type Test = typeof this;
>Test : Symbol(Test, Decl(typeofThis.ts, 139, 34))
}
}

View file

@ -377,3 +377,48 @@ class Test11 {
}
}
}
class Tests12 {
>Tests12 : Tests12
test1() { // OK
>test1 : () => void
type Test = typeof this;
>Test : this
>this : any
}
test2() { // OK
>test2 : () => void
for (;;) {}
type Test = typeof this;
>Test : this
>this : any
}
test3() { // expected no compile errors
>test3 : () => void
for (const dummy in []) {}
>dummy : string
>[] : never[]
type Test = typeof this;
>Test : this
>this : any
}
test4() { // expected no compile errors
>test4 : () => void
for (const dummy of []) {}
>dummy : never
>[] : never[]
type Test = typeof this;
>Test : this
>this : any
}
}

View file

@ -122,4 +122,25 @@ class Test11 {
let y: string = o.this.x; // should narrow to string
}
}
}
class Tests12 {
test1() { // OK
type Test = typeof this;
}
test2() { // OK
for (;;) {}
type Test = typeof this;
}
test3() { // expected no compile errors
for (const dummy in []) {}
type Test = typeof this;
}
test4() { // expected no compile errors
for (const dummy of []) {}
type Test = typeof this;
}
}