MCPcopy Create free account
hub / github.com/microsoft/typescript-go / getCommonSupertype

Method getCommonSupertype

internal/checker/inference.go:1496–1521  ·  view source on GitHub ↗
(types []*Type)

Source from the content-addressed store, hash-verified

1494}
1495
1496func (c *Checker) getCommonSupertype(types []*Type) *Type {
1497 if len(types) == 1 {
1498 return types[0]
1499 }
1500 // Remove nullable types from each of the candidates.
1501 primaryTypes := types
1502 if c.strictNullChecks {
1503 primaryTypes = core.SameMap(types, func(t *Type) *Type {
1504 return c.filterType(t, func(u *Type) bool { return u.flags&TypeFlagsNullable == 0 })
1505 })
1506 }
1507 // When the candidate types are all literal types with the same base type, return a union
1508 // of those literal types. Otherwise, return the leftmost type for which no type to the
1509 // right is a supertype.
1510 var supertype *Type
1511 if c.literalTypesWithSameBaseType(primaryTypes) {
1512 supertype = c.getUnionType(primaryTypes)
1513 } else {
1514 supertype = c.getSingleCommonSupertype(primaryTypes)
1515 }
1516 // Add any nullable types that occurred in the candidates back to the result.
1517 if core.Same(primaryTypes, types) {
1518 return supertype
1519 }
1520 return c.getNullableType(supertype, c.getCombinedTypeFlags(types)&TypeFlagsNullable)
1521}
1522
1523func (c *Checker) getSingleCommonSupertype(types []*Type) *Type {
1524 // First, find the leftmost type for which no type to the right is a strict supertype, and if that

Callers 1

getCovariantInferenceMethod · 0.95

Calls 9

filterTypeMethod · 0.95
getUnionTypeMethod · 0.95
getNullableTypeMethod · 0.95
getCombinedTypeFlagsMethod · 0.95
SameMapFunction · 0.92
SameFunction · 0.92
lenFunction · 0.85

Tested by

no test coverage detected