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

Function conditionalTypeToTypeNode

test/fixtures/snapshot/typescript.js:52631–52665  ·  view source on GitHub ↗
(type)

Source from the content-addressed store, hash-verified

52629 }
52630 return ts.Debug.fail("Should be unreachable.");
52631 function conditionalTypeToTypeNode(type) {
52632 var checkTypeNode = typeToTypeNodeHelper(type.checkType, context);
52633 context.approximateLength += 15;
52634 if (context.flags & 4 /* NodeBuilderFlags.GenerateNamesForShadowedTypeParams */ && type.root.isDistributive && !(type.checkType.flags & 262144 /* TypeFlags.TypeParameter */)) {
52635 var newParam = createTypeParameter(createSymbol(262144 /* SymbolFlags.TypeParameter */, "T"));
52636 var name = typeParameterToName(newParam, context);
52637 var newTypeVariable = ts.factory.createTypeReferenceNode(name);
52638 context.approximateLength += 37; // 15 each for two added conditionals, 7 for an added infer type
52639 var newMapper = prependTypeMapping(type.root.checkType, newParam, type.combinedMapper || type.mapper);
52640 var saveInferTypeParameters_1 = context.inferTypeParameters;
52641 context.inferTypeParameters = type.root.inferTypeParameters;
52642 var extendsTypeNode_1 = typeToTypeNodeHelper(instantiateType(type.root.extendsType, newMapper), context);
52643 context.inferTypeParameters = saveInferTypeParameters_1;
52644 var trueTypeNode_1 = typeToTypeNodeOrCircularityElision(instantiateType(getTypeFromTypeNode(type.root.node.trueType), newMapper));
52645 var falseTypeNode_1 = typeToTypeNodeOrCircularityElision(instantiateType(getTypeFromTypeNode(type.root.node.falseType), newMapper));
52646 // outermost conditional makes `T` a type parameter, allowing the inner conditionals to be distributive
52647 // second conditional makes `T` have `T & checkType` substitution, so it is correctly usable as the checkType
52648 // inner conditional runs the check the user provided on the check type (distributively) and returns the result
52649 // checkType extends infer T ? T extends checkType ? T extends extendsType<T> ? trueType<T> : falseType<T> : never : never;
52650 // this is potentially simplifiable to
52651 // checkType extends infer T ? T extends checkType & extendsType<T> ? trueType<T> : falseType<T> : never;
52652 // but that may confuse users who read the output more.
52653 // On the other hand,
52654 // checkType extends infer T extends checkType ? T extends extendsType<T> ? trueType<T> : falseType<T> : never;
52655 // may also work with `infer ... extends ...` in, but would produce declarations only compatible with the latest TS.
52656 return ts.factory.createConditionalTypeNode(checkTypeNode, ts.factory.createInferTypeNode(ts.factory.createTypeParameterDeclaration(/*modifiers*/ undefined, ts.factory.cloneNode(newTypeVariable.typeName))), ts.factory.createConditionalTypeNode(ts.factory.createTypeReferenceNode(ts.factory.cloneNode(name)), typeToTypeNodeHelper(type.checkType, context), ts.factory.createConditionalTypeNode(newTypeVariable, extendsTypeNode_1, trueTypeNode_1, falseTypeNode_1), ts.factory.createKeywordTypeNode(143 /* SyntaxKind.NeverKeyword */)), ts.factory.createKeywordTypeNode(143 /* SyntaxKind.NeverKeyword */));
52657 }
52658 var saveInferTypeParameters = context.inferTypeParameters;
52659 context.inferTypeParameters = type.root.inferTypeParameters;
52660 var extendsTypeNode = typeToTypeNodeHelper(type.extendsType, context);
52661 context.inferTypeParameters = saveInferTypeParameters;
52662 var trueTypeNode = typeToTypeNodeOrCircularityElision(getTrueTypeFromConditionalType(type));
52663 var falseTypeNode = typeToTypeNodeOrCircularityElision(getFalseTypeFromConditionalType(type));
52664 return ts.factory.createConditionalTypeNode(checkTypeNode, extendsTypeNode, trueTypeNode, falseTypeNode);
52665 }
52666 function typeToTypeNodeOrCircularityElision(type) {
52667 var _a, _b, _c;
52668 if (type.flags & 1048576 /* TypeFlags.Union */) {

Callers 1

typeToTypeNodeHelperFunction · 0.85

Calls 10

typeToTypeNodeHelperFunction · 0.85
createTypeParameterFunction · 0.85
createSymbolFunction · 0.85
typeParameterToNameFunction · 0.85
prependTypeMappingFunction · 0.85
instantiateTypeFunction · 0.85
getTypeFromTypeNodeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…