Skip to content

Commit 88adf89

Browse files
sandersntypescript-bot
authored andcommitted
Cherry-pick PR microsoft#35639 into release-3.7
Component commits: 7c31be3 Fix binding of this-assignments w/computed properties Top-level this-assignments do not support computed properties. But the binder forgets to check for computed properties and tries to bind them normally. This hits a helpful assert. This change stop binding this-properties with computed properties at the top-level. There's nothing sensible we could do with them; we're unable to late-bind entries to the global scope or to modules.
1 parent 79facc0 commit 88adf89

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,10 @@ namespace ts {
27442744
break;
27452745
case SyntaxKind.SourceFile:
27462746
// this.property = assignment in a source file -- declare symbol in exports for a module, in locals for a script
2747-
if ((thisContainer as SourceFile).commonJsModuleIndicator) {
2747+
if (hasDynamicName(node)) {
2748+
break;
2749+
}
2750+
else if ((thisContainer as SourceFile).commonJsModuleIndicator) {
27482751
declareSymbol(thisContainer.symbol.exports!, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
27492752
}
27502753
else {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js(1,1): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof globalThis'.
2+
No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.
3+
4+
5+
==== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js (1 errors) ====
6+
this["a" + "b"] = 0
7+
~~~~~~~~~~~~~~~
8+
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof globalThis'.
9+
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'typeof globalThis'.
10+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
2+
this["a" + "b"] = 0
3+
>this : Symbol(globalThis)
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/salsa/thisPropertyAssignmentComputed.js ===
2+
this["a" + "b"] = 0
3+
>this["a" + "b"] = 0 : 0
4+
>this["a" + "b"] : any
5+
>this : typeof globalThis
6+
>"a" + "b" : string
7+
>"a" : "a"
8+
>"b" : "b"
9+
>0 : 0
10+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @allowjs: true
2+
// @checkjs: true
3+
// @noemit: true
4+
// @strict: true
5+
// @filename: thisPropertyAssignmentComputed.js
6+
this["a" + "b"] = 0

0 commit comments

Comments
 (0)