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

Function narrowTypeByTypeof

test/fixtures/snapshot/typescript.js:71032–71058  ·  view source on GitHub ↗
(type, typeOfExpr, operator, literal, assumeTrue)

Source from the content-addressed store, hash-verified

71030 return type;
71031 }
71032 function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) {
71033 // We have '==', '!=', '===', or !==' operator with 'typeof xxx' and string literal operands
71034 if (operator === 35 /* SyntaxKind.ExclamationEqualsToken */ || operator === 37 /* SyntaxKind.ExclamationEqualsEqualsToken */) {
71035 assumeTrue = !assumeTrue;
71036 }
71037 var target = getReferenceCandidate(typeOfExpr.expression);
71038 if (!isMatchingReference(reference, target)) {
71039 if (strictNullChecks && optionalChainContainsReference(target, reference) && assumeTrue === (literal.text !== "undefined")) {
71040 return getTypeWithFacts(type, 2097152 /* TypeFacts.NEUndefinedOrNull */);
71041 }
71042 return type;
71043 }
71044 if (type.flags & 1 /* TypeFlags.Any */ && literal.text === "function") {
71045 return type;
71046 }
71047 if (assumeTrue && type.flags & 2 /* TypeFlags.Unknown */ && literal.text === "object") {
71048 // The non-null unknown type is used to track whether a previous narrowing operation has removed the null type
71049 // from the unknown type. For example, the expression `x && typeof x === 'object'` first narrows x to the non-null
71050 // unknown type, and then narrows that to the non-primitive type.
71051 return type === nonNullUnknownType ? nonPrimitiveType : getUnionType([nonPrimitiveType, nullType]);
71052 }
71053 var facts = assumeTrue ?
71054 typeofEQFacts.get(literal.text) || 128 /* TypeFacts.TypeofEQHostObject */ :
71055 typeofNEFacts.get(literal.text) || 32768 /* TypeFacts.TypeofNEHostObject */;
71056 var impliedType = getImpliedTypeFromTypeofGuard(type, literal.text);
71057 return getTypeWithFacts(assumeTrue && impliedType ? mapType(type, narrowUnionMemberByTypeof(impliedType)) : type, facts);
71058 }
71059 function narrowTypeBySwitchOptionalChainContainment(type, switchStatement, clauseStart, clauseEnd, clauseCheck) {
71060 var everyClauseChecks = clauseStart !== clauseEnd && ts.every(getSwitchClauseTypes(switchStatement).slice(clauseStart, clauseEnd), clauseCheck);
71061 return everyClauseChecks ? getTypeWithFacts(type, 2097152 /* TypeFacts.NEUndefinedOrNull */) : type;

Callers 1

Calls 9

getReferenceCandidateFunction · 0.85
isMatchingReferenceFunction · 0.85
getTypeWithFactsFunction · 0.85
getUnionTypeFunction · 0.85
mapTypeFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…