Skip to content

Commit a990163

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 with the properties of the object type made optional. A new type with new properties was made 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.
1 parent 4a377f9 commit a990163

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 15 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,13 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1390513907
if secondType != nil {
1390613908
return t
1390713909
}
13910+
// The empty constituents only make the properties of firstType optional,
13911+
// so the result is determined by firstType and readonly, and is interned
13912+
// by them.
13913+
key := mergedEmptyObjectKey{t: firstType, readonly: readonly}
13914+
if existing := c.mergedEmptyObjectTypes[key]; existing != nil {
13915+
return existing
13916+
}
1390813917
// gets the type as if it had been spread, but where everything in the spread is made optional
1390913918
members := ast.NewSymbolTable()
1391013919
for _, prop := range c.getPropertiesOfType(firstType) {
@@ -13928,9 +13937,15 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1392813937
}
1392913938
spread := c.newAnonymousType(firstType.symbol, members, nil, nil, c.getIndexInfosOfType(firstType))
1393013939
spread.objectFlags |= ObjectFlagsObjectLiteral | ObjectFlagsContainsObjectOrArrayLiteral
13940+
c.mergedEmptyObjectTypes[key] = spread
1393113941
return spread
1393213942
}
1393313943

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

‎tsc/testdata/baselines/reference/compiler/spreadUnionOfObjectAndEmptyObjectOrder(checkorder=isolated).types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ declare const b: Pair<typeof x, 2>;
4343
>x : { a?: number | undefined; }
4444

4545
const z = cond ? a : b;
46-
>z : Pair<{ a?: number | undefined; }, 2> | Pair<{ a?: number | undefined; }, 1>
47-
>cond ? a : b : Pair<{ a?: number | undefined; }, 2> | Pair<{ a?: number | undefined; }, 1>
46+
>z : Pair<{ a?: number | undefined; }, 1> | Pair<{ a?: number | undefined; }, 2>
47+
>cond ? a : b : Pair<{ a?: number | undefined; }, 1> | Pair<{ a?: number | undefined; }, 2>
4848
>cond : boolean
4949
>a : Pair<{ a?: number | undefined; }, 1>
5050
>b : Pair<{ a?: number | undefined; }, 2>

‎tsc/testdata/baselines/reference/compiler/spreadUnionOfObjectAndEmptyObjectOrder(checkorder=typesfirst).types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ declare const b: Pair<typeof x, 2>;
4343
>x : { a?: number | undefined; }
4444

4545
const z = cond ? a : b;
46-
>z : Pair<{ a?: number | undefined; }, 2> | Pair<{ a?: number | undefined; }, 1>
47-
>cond ? a : b : Pair<{ a?: number | undefined; }, 2> | Pair<{ a?: number | undefined; }, 1>
46+
>z : Pair<{ a?: number | undefined; }, 1> | Pair<{ a?: number | undefined; }, 2>
47+
>cond ? a : b : Pair<{ a?: number | undefined; }, 1> | Pair<{ a?: number | undefined; }, 2>
4848
>cond : boolean
4949
>a : Pair<{ a?: number | undefined; }, 1>
5050
>b : Pair<{ a?: number | undefined; }, 2>

0 commit comments

Comments
 (0)