Skip to content
Open
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
4 changes: 1 addition & 3 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26723,9 +26723,7 @@ func (c *Checker) removeSubtypes(types []*Type, hasObjectTypes bool) []*Type {
var keyProperty *ast.Symbol
var keyPropertyType *Type
if source.flags&(TypeFlagsObject|TypeFlagsIntersection|TypeFlagsInstantiableNonPrimitive) != 0 {
keyProperty = core.Find(c.getPropertiesOfType(source), func(p *ast.Symbol) bool {
return isUnitType(c.getTypeOfSymbol(p))
})
keyProperty = core.Find(c.getPropertiesOfType(source), c.isPropertyOfUnitType)
}
if keyProperty != nil {
keyPropertyType = c.getRegularTypeOfLiteralType(c.getTypeOfSymbol(keyProperty))
Expand Down
37 changes: 36 additions & 1 deletion tsc/internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ func (c *Checker) getKeyPropertyCandidateName(types []*Type) string {
for _, t := range types {
if t.flags&(TypeFlagsObject|TypeFlagsInstantiableNonPrimitive) != 0 {
for _, p := range c.getPropertiesOfType(t) {
if isUnitType(c.getTypeOfSymbol(p)) {
if c.isPropertyOfUnitType(p) {
return p.Name()
}
}
Expand All @@ -1165,6 +1165,41 @@ func (c *Checker) getKeyPropertyCandidateName(types []*Type) string {
return ""
}

// Reports whether a property has a unit type. It skips resolving the type of a method, which is a function type.
// For an instantiated or mapped property whose type is not resolved yet, it first checks the type
// the property is instantiated from, and skips the instantiation when that type already rules out a unit type.
func (c *Checker) isPropertyOfUnitType(prop *ast.Symbol) bool {
// A method, including an object literal method, is typed as a function type
if prop.Flags()&ast.SymbolFlagsMethod != 0 {
return false
}

checkFlags := prop.CheckFlags()
Comment thread
Andarist marked this conversation as resolved.
Resolved
if checkFlags&(ast.CheckFlagsInstantiated|ast.CheckFlagsMapped) != 0 {
if links := c.valueSymbolLinks.Get(prop); links.resolvedType == nil {
var uninstantiated *Type
if checkFlags&ast.CheckFlagsInstantiated != 0 {
uninstantiated = c.getTypeOfSymbol(links.target)
} else {
mappedType := links.getContainingType()
uninstantiated = c.getTemplateTypeFromMappedType(core.OrElse(mappedType.AsMappedType().target, mappedType))
}
// An object type other than a mapped type or a generic tuple type never instantiates to a unit type or to never,
// so neither does a union containing one. A homomorphic mapped type instantiated for a primitive is that
// primitive. A generic tuple type has a variadic element, like [string, ...T, number], and it normalizes to never
// when that element is instantiated to never. The tuple then drops out of a union, so with T = never the union
// [string, ...T, number] | K is just K, which can be a unit type.
if someType(uninstantiated, func(t *Type) bool {
return t.flags&TypeFlagsObject != 0 && t.objectFlags&ObjectFlagsMapped == 0 && !isGenericTupleType(t)
}) {
return false
}
}
}

return isUnitType(c.getTypeOfSymbol(prop))
Comment thread
Andarist marked this conversation as resolved.
Resolved
}

// Given a set of constituent types and a property name, create and return a map keyed by the literal
// types of the property by that name in each constituent type. No map is returned if some key property
// has a non-literal type or if less than 10 or less than 50% of the constituents have a unique key.
Expand Down