MCPcopy Index your code
hub / github.com/nodejs/node / narrowTypeByInstanceof

Function narrowTypeByInstanceof

test/fixtures/snapshot/typescript.js:71241–71280  ·  view source on GitHub ↗
(type, expr, assumeTrue)

Source from the content-addressed store, hash-verified

71239 }
71240 }
71241 function narrowTypeByInstanceof(type, expr, assumeTrue) {
71242 var left = getReferenceCandidate(expr.left);
71243 if (!isMatchingReference(reference, left)) {
71244 if (assumeTrue && strictNullChecks && optionalChainContainsReference(left, reference)) {
71245 return getTypeWithFacts(type, 2097152 /* TypeFacts.NEUndefinedOrNull */);
71246 }
71247 return type;
71248 }
71249 // Check that right operand is a function type with a prototype property
71250 var rightType = getTypeOfExpression(expr.right);
71251 if (!isTypeDerivedFrom(rightType, globalFunctionType)) {
71252 return type;
71253 }
71254 var targetType;
71255 var prototypeProperty = getPropertyOfType(rightType, "prototype");
71256 if (prototypeProperty) {
71257 // Target type is type of the prototype property
71258 var prototypePropertyType = getTypeOfSymbol(prototypeProperty);
71259 if (!isTypeAny(prototypePropertyType)) {
71260 targetType = prototypePropertyType;
71261 }
71262 }
71263 // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function'
71264 if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) {
71265 return type;
71266 }
71267 if (!targetType) {
71268 var constructSignatures = getSignaturesOfType(rightType, 1 /* SignatureKind.Construct */);
71269 targetType = constructSignatures.length ?
71270 getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })) :
71271 emptyObjectType;
71272 }
71273 // We can't narrow a union based off instanceof without negated types see #31576 for more info
71274 if (!assumeTrue && rightType.flags & 1048576 /* TypeFlags.Union */) {
71275 var nonConstructorTypeInUnion = ts.find(rightType.types, function (t) { return !isConstructorType(t); });
71276 if (!nonConstructorTypeInUnion)
71277 return type;
71278 }
71279 return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
71280 }
71281 function getNarrowedType(type, candidate, assumeTrue, isRelated) {
71282 if (!assumeTrue) {
71283 return filterType(type, function (t) { return !isRelated(t, candidate); });

Callers 1

Calls 15

getReferenceCandidateFunction · 0.85
isMatchingReferenceFunction · 0.85
getTypeWithFactsFunction · 0.85
getTypeOfExpressionFunction · 0.85
isTypeDerivedFromFunction · 0.85
getPropertyOfTypeFunction · 0.85
getTypeOfSymbolFunction · 0.85
isTypeAnyFunction · 0.85
getSignaturesOfTypeFunction · 0.85
getUnionTypeFunction · 0.85
getReturnTypeOfSignatureFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…