Skip to content

Commit 5a79252

Browse files
committed
Track computed property name cycles on the resolution stack
A computed property name stored a placeholder type while its expression was checked, and returned the placeholder when re-entered. That kept the cycle off the resolution stack, so the computations in between did not fail, and the placeholder `{}` escaped into messages when checking entered through the property name, while `any` did when it entered through the variable it references. In blockScopedBindingUsedBeforeDef, the error read "Type '{}' cannot be used as an index type" or "Type 'any' ..." depending on what was checked first. Resolve computed property names on the resolution stack, failing with the error type on re-entry like other circular resolutions.
1 parent 620f184 commit 5a79252

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const (
6565
TypeSystemPropertyNameWriteType
6666
TypeSystemPropertyNameInitializerIsUndefined
6767
TypeSystemPropertyNameAliasTarget
68+
TypeSystemPropertyNameComputedPropertyName
6869
)
6970

7071
type TypeResolution struct {
@@ -19432,6 +19433,8 @@ func (c *Checker) typeResolutionHasProperty(r *TypeResolution) bool {
1943219433
return c.valueSymbolLinks.Get(r.target.(*ast.Symbol)).getWriteType() != nil
1943319434
case TypeSystemPropertyNameAliasTarget:
1943419435
return c.aliasSymbolLinks.Get(r.target.(*ast.Symbol)).aliasTarget != nil
19436+
case TypeSystemPropertyNameComputedPropertyName:
19437+
return c.typeNodeLinks.Get(r.target.(*ast.Node)).resolvedType != nil
1943519438
}
1943619439
panic("Unhandled case in typeResolutionHasProperty")
1943719440
}
@@ -27749,12 +27752,19 @@ func isInvalidComputedPropertyName(node *ast.Node) bool {
2774927752
func (c *Checker) checkComputedPropertyName(node *ast.Node) *Type {
2775027753
links := c.typeNodeLinks.Get(node)
2775127754
if links.resolvedType == nil {
27752-
links.resolvedType = c.circularConstraintType
2775327755
if isInvalidComputedPropertyName(node) {
2775427756
links.resolvedType = c.errorType
2775527757
return links.resolvedType
2775627758
}
27757-
links.resolvedType = c.checkExpression(node.Expression())
27759+
if !c.pushTypeResolution(node, TypeSystemPropertyNameComputedPropertyName) {
27760+
return c.errorType
27761+
}
27762+
t := c.checkExpression(node.Expression())
27763+
c.popTypeResolution()
27764+
if links.resolvedType != nil {
27765+
return links.resolvedType
27766+
}
27767+
links.resolvedType = t
2775827768
// This will allow types number, string, symbol or any. It will also allow enums, the unknown
2775927769
// type, and any union of these types (like string | number).
2776027770
if links.resolvedType.flags&TypeFlagsNullable != 0 ||
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
blockScopedBindingUsedBeforeDef.ts(2,12): error TS2448: Block-scoped variable 'a' used before its declaration.
2-
blockScopedBindingUsedBeforeDef.ts(2,12): error TS2538: Type '{}' cannot be used as an index type.
2+
blockScopedBindingUsedBeforeDef.ts(2,12): error TS2538: Type 'any' cannot be used as an index type.
33
blockScopedBindingUsedBeforeDef.ts(5,12): error TS2448: Block-scoped variable 'a' used before its declaration.
4-
blockScopedBindingUsedBeforeDef.ts(5,12): error TS2538: Type '{}' cannot be used as an index type.
4+
blockScopedBindingUsedBeforeDef.ts(5,12): error TS2538: Type 'any' cannot be used as an index type.
55
blockScopedBindingUsedBeforeDef.ts(8,7): error TS2448: Block-scoped variable 'b' used before its declaration.
6-
blockScopedBindingUsedBeforeDef.ts(8,7): error TS2538: Type '{}' cannot be used as an index type.
6+
blockScopedBindingUsedBeforeDef.ts(8,7): error TS2538: Type 'any' cannot be used as an index type.
77

88

99
==== blockScopedBindingUsedBeforeDef.ts (6 errors) ====
@@ -13,20 +13,20 @@ blockScopedBindingUsedBeforeDef.ts(8,7): error TS2538: Type '{}' cannot be used
1313
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
1414
!!! related TS2728 blockScopedBindingUsedBeforeDef.ts:2:16: 'a' is declared here.
1515
~
16-
!!! error TS2538: Type '{}' cannot be used as an index type.
16+
!!! error TS2538: Type 'any' cannot be used as an index type.
1717

1818
// 2:
1919
for (let {[a]: a} = { }; false; ) continue;
2020
~
2121
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
2222
!!! related TS2728 blockScopedBindingUsedBeforeDef.ts:5:16: 'a' is declared here.
2323
~
24-
!!! error TS2538: Type '{}' cannot be used as an index type.
24+
!!! error TS2538: Type 'any' cannot be used as an index type.
2525

2626
// 3:
2727
let {[b]: b} = { };
2828
~
2929
!!! error TS2448: Block-scoped variable 'b' used before its declaration.
3030
!!! related TS2728 blockScopedBindingUsedBeforeDef.ts:8:11: 'b' is declared here.
3131
~
32-
!!! error TS2538: Type '{}' cannot be used as an index type.
32+
!!! error TS2538: Type 'any' cannot be used as an index type.

0 commit comments

Comments
 (0)