Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Comment thread
gabritto marked this conversation as resolved.
Resolved
// 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
Comment thread
Copilot marked this conversation as resolved.
Resolved
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())
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down
Loading