Skip to content

Commit df57558

Browse files
committed
Intern spreads of a union of an object type and empty objects
Spreading a union of one object type and empty objects makes an object type whose properties are those of the object type made optional, and made a new one with new properties every time, so spreading the same union twice yielded types that could be told apart only by their properties' symbol ids (seen in tanstack-router). They are now interned by the object type and whether the spread is readonly, which determine them.
1 parent 56b1200 commit df57558

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ type Checker struct {
681681
deferredConditionalTypes map[deferredConditionalTypeKey]*Type
682682
emptyObjectTypesFromStringLiterals map[CacheHashKey]*Type
683683
bindingElementOfMember map[*ast.Symbol]*ast.Node // The binding element declaring a member of a type implied by a binding pattern
684+
mergedEmptyObjectTypes map[mergedEmptyObjectKey]*Type
684685
spreadSymbols map[spreadSymbolKey]*ast.Symbol
685686
symbolsWithType map[symbolWithTypeKey]*ast.Symbol
686687
paddedProperties map[symbolWithTypeKey]*ast.Symbol
@@ -1031,6 +1032,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10311032
c.deferredConditionalTypes = make(map[deferredConditionalTypeKey]*Type)
10321033
c.emptyObjectTypesFromStringLiterals = make(map[CacheHashKey]*Type)
10331034
c.bindingElementOfMember = make(map[*ast.Symbol]*ast.Node)
1035+
c.mergedEmptyObjectTypes = make(map[mergedEmptyObjectKey]*Type)
10341036
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10351037
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10361038
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)
@@ -13905,6 +13907,12 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1390513907
if secondType != nil {
1390613908
return t
1390713909
}
13910+
// The result is determined by the one type that is not empty, so it is
13911+
// interned by that type.
13912+
key := mergedEmptyObjectKey{t: firstType, readonly: readonly}
13913+
if existing := c.mergedEmptyObjectTypes[key]; existing != nil {
13914+
return existing
13915+
}
1390813916
// gets the type as if it had been spread, but where everything in the spread is made optional
1390913917
members := ast.NewSymbolTable()
1391013918
for _, prop := range c.getPropertiesOfType(firstType) {
@@ -13928,9 +13936,15 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1392813936
}
1392913937
spread := c.newAnonymousType(firstType.symbol, members, nil, nil, c.getIndexInfosOfType(firstType))
1393013938
spread.objectFlags |= ObjectFlagsObjectLiteral | ObjectFlagsContainsObjectOrArrayLiteral
13939+
c.mergedEmptyObjectTypes[key] = spread
1393113940
return spread
1393213941
}
1393313942

13943+
type mergedEmptyObjectKey struct {
13944+
t *Type
13945+
readonly bool
13946+
}
13947+
1393413948
// We approximate own properties as non-methods plus methods that are inside the object literal
1393513949
func (c *Checker) isSpreadableProperty(prop *ast.Symbol) bool {
1393613950
return !core.Some(prop.Declarations(), ast.IsPrivateIdentifierClassElementDeclaration) && prop.Flags()&(ast.SymbolFlagsMethod|ast.SymbolFlagsGetAccessor|ast.SymbolFlagsSetAccessor) == 0 ||

0 commit comments

Comments
 (0)