Skip to content

Commit ccfb1a6

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 e88c5f8 commit ccfb1a6

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
@@ -682,6 +682,7 @@ type Checker struct {
682682
deferredConditionalTypes map[deferredConditionalTypeKey]*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
@@ -1032,6 +1033,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10321033
c.deferredConditionalTypes = make(map[deferredConditionalTypeKey]*Type)
10331034
c.emptyObjectTypesFromStringLiterals = make(map[CacheHashKey]*Type)
10341035
c.bindingElementOfMember = make(map[*ast.Symbol]*ast.Node)
1036+
c.mergedEmptyObjectTypes = make(map[mergedEmptyObjectKey]*Type)
10351037
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10361038
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10371039
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)
@@ -13906,6 +13908,13 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1390613908
if secondType != nil {
1390713909
return t
1390813910
}
13911+
// The empty constituents only make the properties of firstType optional,
13912+
// so the result is determined by firstType and readonly, and is interned
13913+
// by them.
13914+
key := mergedEmptyObjectKey{t: firstType, readonly: readonly}
13915+
if existing := c.mergedEmptyObjectTypes[key]; existing != nil {
13916+
return existing
13917+
}
1390913918
// gets the type as if it had been spread, but where everything in the spread is made optional
1391013919
members := ast.NewSymbolTable()
1391113920
for _, prop := range c.getPropertiesOfType(firstType) {
@@ -13929,9 +13938,15 @@ func (c *Checker) tryMergeUnionOfObjectTypeAndEmptyObject(t *Type, readonly bool
1392913938
}
1393013939
spread := c.newAnonymousType(firstType.symbol, members, nil, nil, c.getIndexInfosOfType(firstType))
1393113940
spread.objectFlags |= ObjectFlagsObjectLiteral | ObjectFlagsContainsObjectOrArrayLiteral
13941+
c.mergedEmptyObjectTypes[key] = spread
1393213942
return spread
1393313943
}
1393413944

13945+
type mergedEmptyObjectKey struct {
13946+
t *Type
13947+
readonly bool
13948+
}
13949+
1393513950
// We approximate own properties as non-methods plus methods that are inside the object literal
1393613951
func (c *Checker) isSpreadableProperty(prop *ast.Symbol) bool {
1393713952
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)