Skip to content

Commit cd8efe5

Browse files
committed
Intern object types inferred from string literal keys
Inferring from string literals to keyof T infers to T from an object type with an any-typed property per literal, and made a new one every time, so inferring from the same literals twice yielded types that could be told apart only by type id (seen in tanstack-router). They are now interned by the literals and whether the source includes string, which determine them.
1 parent 61c1856 commit cd8efe5

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ type Checker struct {
679679
spreadTypes map[CacheHashKey]*Type
680680
objectContentTypes map[CacheHashKey]*Type
681681
deferredConditionalTypes map[deferredConditionalTypeKey]*Type
682+
emptyObjectTypesFromStringLiterals map[CacheHashKey]*Type
682683
spreadSymbols map[spreadSymbolKey]*ast.Symbol
683684
symbolsWithType map[symbolWithTypeKey]*ast.Symbol
684685
paddedProperties map[symbolWithTypeKey]*ast.Symbol
@@ -1027,6 +1028,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10271028
c.spreadTypes = make(map[CacheHashKey]*Type)
10281029
c.objectContentTypes = make(map[CacheHashKey]*Type)
10291030
c.deferredConditionalTypes = make(map[deferredConditionalTypeKey]*Type)
1031+
c.emptyObjectTypesFromStringLiterals = make(map[CacheHashKey]*Type)
10301032
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10311033
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10321034
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)

‎tsc/internal/checker/inference.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,28 @@ func (c *Checker) isTypeCloselyMatchedBy(s *Type, t *Type) bool {
12431243

12441244
// Create an object with properties named in the string literal type. Every property has type `any`.
12451245
func (c *Checker) createEmptyObjectTypeFromStringLiteral(t *Type) *Type {
1246+
// The result is determined by the string literals in t and whether t
1247+
// includes string, so it is interned by those.
1248+
var b keyBuilder
1249+
for _, u := range t.Distributed() {
1250+
if u.flags&TypeFlagsStringLiteral != 0 {
1251+
b.writeString(getStringLiteralValue(u))
1252+
b.writeByte(0)
1253+
if u.symbol != nil {
1254+
b.writeByte(1)
1255+
b.writeSymbol(u.symbol)
1256+
} else {
1257+
b.writeByte(0)
1258+
}
1259+
}
1260+
}
1261+
if t.flags&TypeFlagsString != 0 {
1262+
b.writeByte(2)
1263+
}
1264+
key := b.hash()
1265+
if cached := c.emptyObjectTypesFromStringLiterals[key]; cached != nil {
1266+
return cached
1267+
}
12461268
members := ast.NewSymbolTable()
12471269
for _, t := range t.Distributed() {
12481270
if t.flags&TypeFlagsStringLiteral == 0 {
@@ -1261,7 +1283,9 @@ func (c *Checker) createEmptyObjectTypeFromStringLiteral(t *Type) *Type {
12611283
if t.flags&TypeFlagsString != 0 {
12621284
indexInfos = []*IndexInfo{c.newIndexInfo(c.stringType, c.emptyObjectType, false /*isReadonly*/, nil, nil)}
12631285
}
1264-
return c.newAnonymousType(nil, members, nil, nil, indexInfos)
1286+
result := c.newAnonymousType(nil, members, nil, nil, indexInfos)
1287+
c.emptyObjectTypesFromStringLiterals[key] = result
1288+
return result
12651289
}
12661290

12671291
func (c *Checker) newInferenceContext(typeParameters []*Type, signature *Signature, flags InferenceFlags, compareTypes TypeComparer) *InferenceContext {

0 commit comments

Comments
 (0)