Skip to content

allow typeof this after for-loops #46181

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 1 commit into from
Oct 27, 2021
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
7 changes: 5 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22471,8 +22471,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:
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/typeofThis.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
39 changes: 39 additions & 0 deletions tests/baselines/reference/typeofThis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
}());
39 changes: 39 additions & 0 deletions tests/baselines/reference/typeofThis.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
45 changes: 45 additions & 0 deletions tests/baselines/reference/typeofThis.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}