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

Method getAssignmentReducedTypeWorker

internal/checker/flow.go:2390–2407  ·  view source on GitHub ↗
(declaredType *Type, assignedType *Type)

Source from the content-addressed store, hash-verified

2388}
2389
2390func (c *Checker) getAssignmentReducedTypeWorker(declaredType *Type, assignedType *Type) *Type {
2391 filteredType := c.filterType(declaredType, func(t *Type) bool {
2392 return c.typeMaybeAssignableTo(assignedType, t)
2393 })
2394 // Ensure that we narrow to fresh types if the assignment is a fresh boolean literal type.
2395 reducedType := filteredType
2396 if assignedType.flags&TypeFlagsBooleanLiteral != 0 && isFreshLiteralType(assignedType) {
2397 reducedType = c.mapType(filteredType, c.getFreshTypeOfLiteralType)
2398 }
2399 // Our crude heuristic produces an invalid result in some cases: see GH#26130.
2400 // For now, when that happens, we give up and don't narrow at all. (This also
2401 // means we'll never narrow for erroneous assignments where the assigned type
2402 // is not assignable to the declared type.)
2403 if c.isTypeAssignableTo(assignedType, reducedType) {
2404 return reducedType
2405 }
2406 return declaredType
2407}
2408
2409func (c *Checker) typeMaybeAssignableTo(source *Type, target *Type) bool {
2410 if source.flags&TypeFlagsUnion == 0 {

Callers 1

Calls 5

filterTypeMethod · 0.95
typeMaybeAssignableToMethod · 0.95
mapTypeMethod · 0.95
isTypeAssignableToMethod · 0.95
isFreshLiteralTypeFunction · 0.85

Tested by

no test coverage detected