From e0a333d0c4d577693b37056055618fb69ca69edc Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Nov 2026 08:29:11 -0800 Subject: [PATCH 1/2] Optimize getPropertiesOfUnionOrIntersectionType --- tsc/internal/checker/checker.go | 35 ++++++++++++++++++++++++++------- tsc/internal/checker/relater.go | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..22479490de43a 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -19399,6 +19399,14 @@ func (c *Checker) getLayoutPropertiesOfType(t *Type) []*ast.Symbol { return layout.layoutProperties } +func (c *Checker) getPropertyCountOfType(t *Type) int { + t = c.getReducedApparentType(t) + if lazy := c.getLazyStateOfType(t); lazy != nil { + return lazy.layout.members.Len() + } + return len(c.getPropertiesOfType(t)) +} + func (c *Checker) getLazyStateOfType(t *Type) *LazyState { if t.flags&TypeFlagsObject != 0 { return c.resolveStructuredTypeMembers(t).getLazyState() @@ -19411,7 +19419,25 @@ func (c *Checker) getPropertiesOfUnionOrIntersectionType(t *Type) []*ast.Symbol if d.resolvedProperties == nil { var checked collections.Set[string] props := []*ast.Symbol{} - for _, current := range d.types { + types := d.types + if t.flags&TypeFlagsUnion != 0 { + // Find the constituent type of the union with the fewest properties and no index signature. This type will be used + // as the representative for checking properties of the union, as all properties must be present in all constituent types. + var smallestUnion *Type + var smallestPropertyCount int + for _, current := range d.types { + if len(c.getIndexInfosOfType(current)) == 0 { + if count := c.getPropertyCountOfType(current); smallestUnion == nil || count < smallestPropertyCount { + smallestUnion = current + smallestPropertyCount = count + } + } + } + if smallestUnion != nil { + types = []*Type{smallestUnion} + } + } + for _, current := range types { for _, prop := range c.getPropertiesOfType(current) { if !checked.Has(prop.Name()) { checked.Add(prop.Name()) @@ -19421,11 +19447,6 @@ func (c *Checker) getPropertiesOfUnionOrIntersectionType(t *Type) []*ast.Symbol } } } - // The properties of a union type are those that are present in all constituent types, so - // we only need to check the properties of the first type without index signature - if t.flags&TypeFlagsUnion != 0 && len(c.getIndexInfosOfType(current)) == 0 { - break - } } d.resolvedProperties = props } @@ -28199,7 +28220,7 @@ func (c *Checker) getPropertyNameFromIndex(indexType *Type, accessNode *ast.Node } func (c *Checker) isStringIndexSignatureOnlyTypeWorker(t *Type) bool { - return t.flags&TypeFlagsObject != 0 && t.objectFlags&(ObjectFlagsClassOrInterface|ObjectFlagsReference) == 0 && !c.isGenericMappedType(t) && len(c.getLayoutPropertiesOfType(t)) == 0 && len(c.getIndexInfosOfType(t)) == 1 && c.getIndexInfoOfType(t, c.stringType) != nil || + return t.flags&TypeFlagsObject != 0 && t.objectFlags&(ObjectFlagsClassOrInterface|ObjectFlagsReference) == 0 && !c.isGenericMappedType(t) && c.getPropertyCountOfType(t) == 0 && len(c.getIndexInfosOfType(t)) == 1 && c.getIndexInfoOfType(t, c.stringType) != nil || t.flags&TypeFlagsUnionOrIntersection != 0 && core.Every(t.Types(), c.isStringIndexSignatureOnlyType) } diff --git a/tsc/internal/checker/relater.go b/tsc/internal/checker/relater.go index 84dd374e84faa..b29a51c6cf46c 100644 --- a/tsc/internal/checker/relater.go +++ b/tsc/internal/checker/relater.go @@ -683,7 +683,7 @@ func (c *Checker) elaborateArrowFunction(node *ast.Node, source *Type, target *T func (c *Checker) isWeakType(t *Type) bool { if t.flags&TypeFlagsObject != 0 { resolved := c.resolveStructuredTypeMembers(t) - return len(resolved.getSignatures()) == 0 && len(resolved.getIndexInfos()) == 0 && len(c.getLayoutPropertiesOfType(t)) > 0 && core.Every(c.getLayoutPropertiesOfType(t), func(p *ast.Symbol) bool { + return len(resolved.getSignatures()) == 0 && len(resolved.getIndexInfos()) == 0 && c.getPropertyCountOfType(t) != 0 && core.Every(c.getLayoutPropertiesOfType(t), func(p *ast.Symbol) bool { return p.Flags()&ast.SymbolFlagsOptional != 0 }) } From 00262615a049d232374a928bd5d4068816ce4275 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Nov 2026 09:04:36 -0800 Subject: [PATCH 2/2] Address CR feedback --- tsc/internal/checker/checker.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 22479490de43a..b37557da783c4 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -19423,18 +19423,21 @@ func (c *Checker) getPropertiesOfUnionOrIntersectionType(t *Type) []*ast.Symbol if t.flags&TypeFlagsUnion != 0 { // Find the constituent type of the union with the fewest properties and no index signature. This type will be used // as the representative for checking properties of the union, as all properties must be present in all constituent types. - var smallestUnion *Type + var smallestType *Type var smallestPropertyCount int for _, current := range d.types { if len(c.getIndexInfosOfType(current)) == 0 { - if count := c.getPropertyCountOfType(current); smallestUnion == nil || count < smallestPropertyCount { - smallestUnion = current + if count := c.getPropertyCountOfType(current); smallestType == nil || count < smallestPropertyCount { + smallestType = current smallestPropertyCount = count + if count == 0 { + break + } } } } - if smallestUnion != nil { - types = []*Type{smallestUnion} + if smallestType != nil { + types = []*Type{smallestType} } } for _, current := range types {