diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..b37557da783c4 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,28 @@ 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 smallestType *Type + var smallestPropertyCount int + for _, current := range d.types { + if len(c.getIndexInfosOfType(current)) == 0 { + if count := c.getPropertyCountOfType(current); smallestType == nil || count < smallestPropertyCount { + smallestType = current + smallestPropertyCount = count + if count == 0 { + break + } + } + } + } + if smallestType != nil { + types = []*Type{smallestType} + } + } + for _, current := range types { for _, prop := range c.getPropertiesOfType(current) { if !checked.Has(prop.Name()) { checked.Add(prop.Name()) @@ -19421,11 +19450,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 +28223,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 }) }