Skip to content

Commit 4c5aab8

Browse files
committed
Avoid circularity error caused by requesting contextual type for late bound expando members
1 parent 246507f commit 4c5aab8

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31536,13 +31536,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3153631536
if (isPossiblyAliasedThisProperty(binaryExpression, kind)) {
3153731537
return getContextualTypeForThisPropertyAssignment(binaryExpression);
3153831538
}
31539+
let nodeSymbol;
31540+
if (canHaveSymbol(binaryExpression.left)) {
31541+
nodeSymbol = binaryExpression.left.symbol;
31542+
if (!nodeSymbol) {
31543+
const resolvedSymbol = getNodeLinks(binaryExpression.left).resolvedSymbol;
31544+
if (resolvedSymbol && getIsLateCheckFlag(resolvedSymbol)) {
31545+
nodeSymbol = resolvedSymbol;
31546+
}
31547+
}
31548+
}
3153931549
// If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
3154031550
// See `bindStaticPropertyAssignment` in `binder.ts`.
31541-
else if (!canHaveSymbol(binaryExpression.left) || !binaryExpression.left.symbol) {
31551+
if (!nodeSymbol) {
3154231552
return getTypeOfExpression(binaryExpression.left);
3154331553
}
3154431554
else {
31545-
const decl = binaryExpression.left.symbol.valueDeclaration;
31555+
const decl = nodeSymbol.valueDeclaration;
3154631556
if (!decl) {
3154731557
return undefined;
3154831558
}
@@ -31565,6 +31575,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3156531575
return undefined;
3156631576
}
3156731577
}
31578+
Debug.assert(canHaveSymbol(binaryExpression.left));
3156831579
return isInJSFile(decl) || decl === binaryExpression.left ? undefined : getTypeOfExpression(binaryExpression.left);
3156931580
}
3157031581
case AssignmentDeclarationKind.ExportsProperty:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/uniqueSymbolExpando1.ts] ////
2+
3+
=== uniqueSymbolExpando1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61214
5+
6+
const TestSymbol: unique symbol = Symbol();
7+
>TestSymbol : Symbol(TestSymbol, Decl(uniqueSymbolExpando1.ts, 2, 5))
8+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
9+
10+
const c = () => {
11+
>c : Symbol(c, Decl(uniqueSymbolExpando1.ts, 4, 5), Decl(uniqueSymbolExpando1.ts, 6, 2), Decl(uniqueSymbolExpando1.ts, 7, 26))
12+
13+
return "Hello, world!";
14+
};
15+
c["testProp"] = ["Hello"];
16+
>c : Symbol(c, Decl(uniqueSymbolExpando1.ts, 4, 5), Decl(uniqueSymbolExpando1.ts, 6, 2), Decl(uniqueSymbolExpando1.ts, 7, 26))
17+
>"testProp" : Symbol(c["testProp"], Decl(uniqueSymbolExpando1.ts, 6, 2))
18+
19+
c[TestSymbol] = ["Hello"];
20+
>c : Symbol(c, Decl(uniqueSymbolExpando1.ts, 4, 5), Decl(uniqueSymbolExpando1.ts, 6, 2), Decl(uniqueSymbolExpando1.ts, 7, 26))
21+
>TestSymbol : Symbol(TestSymbol, Decl(uniqueSymbolExpando1.ts, 2, 5))
22+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/compiler/uniqueSymbolExpando1.ts] ////
2+
3+
=== uniqueSymbolExpando1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61214
5+
6+
const TestSymbol: unique symbol = Symbol();
7+
>TestSymbol : unique symbol
8+
> : ^^^^^^^^^^^^^
9+
>Symbol() : unique symbol
10+
> : ^^^^^^^^^^^^^
11+
>Symbol : SymbolConstructor
12+
> : ^^^^^^^^^^^^^^^^^
13+
14+
const c = () => {
15+
>c : { (): string; testProp: string[]; [TestSymbol]: string[]; }
16+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
>() => { return "Hello, world!";} : { (): string; testProp: string[]; [TestSymbol]: string[]; }
18+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
20+
return "Hello, world!";
21+
>"Hello, world!" : "Hello, world!"
22+
> : ^^^^^^^^^^^^^^^
23+
24+
};
25+
c["testProp"] = ["Hello"];
26+
>c["testProp"] = ["Hello"] : string[]
27+
> : ^^^^^^^^
28+
>c["testProp"] : string[]
29+
> : ^^^^^^^^
30+
>c : { (): string; testProp: string[]; [TestSymbol]: string[]; }
31+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
>"testProp" : "testProp"
33+
> : ^^^^^^^^^^
34+
>["Hello"] : string[]
35+
> : ^^^^^^^^
36+
>"Hello" : "Hello"
37+
> : ^^^^^^^
38+
39+
c[TestSymbol] = ["Hello"];
40+
>c[TestSymbol] = ["Hello"] : string[]
41+
> : ^^^^^^^^
42+
>c[TestSymbol] : string[]
43+
> : ^^^^^^^^
44+
>c : { (): string; testProp: string[]; [TestSymbol]: string[]; }
45+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
>TestSymbol : unique symbol
47+
> : ^^^^^^^^^^^^^
48+
>["Hello"] : string[]
49+
> : ^^^^^^^^
50+
>"Hello" : "Hello"
51+
> : ^^^^^^^
52+

tests/baselines/reference/wellKnownSymbolExpando.types

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function f() {}
88
f[Symbol.iterator] = function() {}
99
>f[Symbol.iterator] = function() {} : () => void
1010
> : ^^^^^^^^^^
11-
>f[Symbol.iterator] : any
11+
>f[Symbol.iterator] : () => void
12+
> : ^^^^^^^^^^
1213
>f : typeof f
1314
> : ^^^^^^^^
1415
>Symbol.iterator : unique symbol
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strict: true
2+
// @target: esnext
3+
// @noEmit: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/61214
6+
7+
const TestSymbol: unique symbol = Symbol();
8+
9+
const c = () => {
10+
return "Hello, world!";
11+
};
12+
c["testProp"] = ["Hello"];
13+
c[TestSymbol] = ["Hello"];

tests/cases/compiler/wellKnownSymbolExpando.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @strict: true
12
// @noEmit: true
23
// @target: esnext
34

0 commit comments

Comments
 (0)