MCPcopy Create free account
hub / github.com/nodejs/node / getAssignmentReducedType

Function getAssignmentReducedType

test/fixtures/snapshot/typescript.js:69564–69582  ·  view source on GitHub ↗
(declaredType, assignedType)

Source from the content-addressed store, hash-verified

69562 // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
69563 // we remove type string.
69564 function getAssignmentReducedType(declaredType, assignedType) {
69565 if (declaredType !== assignedType) {
69566 if (assignedType.flags & 131072 /* TypeFlags.Never */) {
69567 return assignedType;
69568 }
69569 var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); });
69570 if (assignedType.flags & 512 /* TypeFlags.BooleanLiteral */ && isFreshLiteralType(assignedType)) {
69571 reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
69572 }
69573 // Our crude heuristic produces an invalid result in some cases: see GH#26130.
69574 // For now, when that happens, we give up and don't narrow at all. (This also
69575 // means we'll never narrow for erroneous assignments where the assigned type
69576 // is not assignable to the declared type.)
69577 if (isTypeAssignableTo(assignedType, reducedType)) {
69578 return reducedType;
69579 }
69580 }
69581 return declaredType;
69582 }
69583 function isFunctionObjectType(type) {
69584 // We do a quick check for a "bind" property before performing the more expensive subtype
69585 // check. This gives us a quicker out in the common case where an object type is not a function.

Callers 1

getTypeAtFlowAssignmentFunction · 0.85

Calls 5

filterTypeFunction · 0.85
typeMaybeAssignableToFunction · 0.85
isFreshLiteralTypeFunction · 0.85
mapTypeFunction · 0.85
isTypeAssignableToFunction · 0.85

Tested by

no test coverage detected