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

Function narrowTypeByOptionalChainContainment

test/fixtures/snapshot/typescript.js:70971–70988  ·  view source on GitHub ↗
(type, operator, value, assumeTrue)

Source from the content-addressed store, hash-verified

70969 return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
70970 }
70971 function narrowTypeByOptionalChainContainment(type, operator, value, assumeTrue) {
70972 // We are in a branch of obj?.foo === value (or any one of the other equality operators). We narrow obj as follows:
70973 // When operator is === and type of value excludes undefined, null and undefined is removed from type of obj in true branch.
70974 // When operator is !== and type of value excludes undefined, null and undefined is removed from type of obj in false branch.
70975 // When operator is == and type of value excludes null and undefined, null and undefined is removed from type of obj in true branch.
70976 // When operator is != and type of value excludes null and undefined, null and undefined is removed from type of obj in false branch.
70977 // When operator is === and type of value is undefined, null and undefined is removed from type of obj in false branch.
70978 // When operator is !== and type of value is undefined, null and undefined is removed from type of obj in true branch.
70979 // When operator is == and type of value is null or undefined, null and undefined is removed from type of obj in false branch.
70980 // When operator is != and type of value is null or undefined, null and undefined is removed from type of obj in true branch.
70981 var equalsOperator = operator === 34 /* SyntaxKind.EqualsEqualsToken */ || operator === 36 /* SyntaxKind.EqualsEqualsEqualsToken */;
70982 var nullableFlags = operator === 34 /* SyntaxKind.EqualsEqualsToken */ || operator === 35 /* SyntaxKind.ExclamationEqualsToken */ ? 98304 /* TypeFlags.Nullable */ : 32768 /* TypeFlags.Undefined */;
70983 var valueType = getTypeOfExpression(value);
70984 // Note that we include any and unknown in the exclusion test because their domain includes null and undefined.
70985 var removeNullable = equalsOperator !== assumeTrue && everyType(valueType, function (t) { return !!(t.flags & nullableFlags); }) ||
70986 equalsOperator === assumeTrue && everyType(valueType, function (t) { return !(t.flags & (3 /* TypeFlags.AnyOrUnknown */ | nullableFlags)); });
70987 return removeNullable ? getTypeWithFacts(type, 2097152 /* TypeFacts.NEUndefinedOrNull */) : type;
70988 }
70989 function narrowTypeByEquality(type, operator, value, assumeTrue) {
70990 if (type.flags & 1 /* TypeFlags.Any */) {
70991 return type;

Callers 1

Calls 3

getTypeOfExpressionFunction · 0.85
everyTypeFunction · 0.85
getTypeWithFactsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…