Skip to content

Commit b829315

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 f31b430 commit b829315

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
@@ -682,6 +682,7 @@ type Checker struct {
682682
mappedTypeConstituents map[CacheHashKey]*Type
683683
emptyObjectTypesFromStringLiterals map[CacheHashKey]*Type
684684
bindingElementOfMember map[*ast.Symbol]*ast.Node // The binding element declaring a member of a type implied by a binding pattern
685+
mergedEmptyObjectTypes map[mergedEmptyObjectKey]*Type
685686
spreadSymbols map[spreadSymbolKey]*ast.Symbol
686687
symbolsWithType map[symbolWithTypeKey]*ast.Symbol
687688
paddedProperties map[symbolWithTypeKey]*ast.Symbol
@@ -1033,6 +1034,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10331034
c.mappedTypeConstituents = make(map[CacheHashKey]*Type)
10341035
c.emptyObjectTypesFromStringLiterals = make(map[CacheHashKey]*Type)
10351036
c.bindingElementOfMember = make(map[*ast.Symbol]*ast.Node)
1037+
c.mergedEmptyObjectTypes = make(map[mergedEmptyObjectKey]*Type)
10361038
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10371039
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10381040
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)
@@ -13907,6 +13909,12 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1390713909
if secondType != nil {
1390813910
return t
1390913911
}
13912+
// The result is determined by the one type that is not empty, so it is
13913+
// interned by that type.
13914+
key := mergedEmptyObjectKey{t: firstType, readonly: readonly}
13915+
if existing := c.mergedEmptyObjectTypes[key]; existing != nil {
13916+
return existing
13917+
}
1391013918
// gets the type as if it had been spread, but where everything in the spread is made optional
1391113919
members := ast.NewSymbolTable()
1391213920
for _, prop := range c.getPropertiesOfType(firstType) {
@@ -13930,9 +13938,15 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1393013938
}
1393113939
spread := c.newAnonymousType(firstType.symbol, members, nil, nil, c.getIndexInfosOfType(firstType))
1393213940
spread.objectFlags |= ObjectFlagsObjectLiteral | ObjectFlagsContainsObjectOrArrayLiteral
13941+
c.mergedEmptyObjectTypes[key] = spread
1393313942
return spread
1393413943
}
1393513944

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

0 commit comments

Comments
 (0)