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

Function getInferredTypeParameterConstraint

test/fixtures/snapshot/typescript.js:60066–60133  ·  view source on GitHub ↗
(typeParameter, omitTypeReferences)

Source from the content-addressed store, hash-verified

60064 return ts.mapDefined(ts.filter(type.symbol && type.symbol.declarations, ts.isTypeParameterDeclaration), ts.getEffectiveConstraintOfTypeParameter)[0];
60065 }
60066 function getInferredTypeParameterConstraint(typeParameter, omitTypeReferences) {
60067 var _a;
60068 var inferences;
60069 if ((_a = typeParameter.symbol) === null || _a === void 0 ? void 0 : _a.declarations) {
60070 for (var _i = 0, _b = typeParameter.symbol.declarations; _i < _b.length; _i++) {
60071 var declaration = _b[_i];
60072 if (declaration.parent.kind === 190 /* SyntaxKind.InferType */) {
60073 // When an 'infer T' declaration is immediately contained in a type reference node
60074 // (such as 'Foo<infer T>'), T's constraint is inferred from the constraint of the
60075 // corresponding type parameter in 'Foo'. When multiple 'infer T' declarations are
60076 // present, we form an intersection of the inferred constraint types.
60077 var _c = ts.walkUpParenthesizedTypesAndGetParentAndChild(declaration.parent.parent), _d = _c[0], childTypeParameter = _d === void 0 ? declaration.parent : _d, grandParent = _c[1];
60078 if (grandParent.kind === 178 /* SyntaxKind.TypeReference */ && !omitTypeReferences) {
60079 var typeReference = grandParent;
60080 var typeParameters = getTypeParametersForTypeReference(typeReference);
60081 if (typeParameters) {
60082 var index = typeReference.typeArguments.indexOf(childTypeParameter);
60083 if (index < typeParameters.length) {
60084 var declaredConstraint = getConstraintOfTypeParameter(typeParameters[index]);
60085 if (declaredConstraint) {
60086 // Type parameter constraints can reference other type parameters so
60087 // constraints need to be instantiated. If instantiation produces the
60088 // type parameter itself, we discard that inference. For example, in
60089 // type Foo<T extends string, U extends T> = [T, U];
60090 // type Bar<T> = T extends Foo<infer X, infer X> ? Foo<X, X> : T;
60091 // the instantiated constraint for U is X, so we discard that inference.
60092 var mapper = createTypeMapper(typeParameters, getEffectiveTypeArguments(typeReference, typeParameters));
60093 var constraint = instantiateType(declaredConstraint, mapper);
60094 if (constraint !== typeParameter) {
60095 inferences = ts.append(inferences, constraint);
60096 }
60097 }
60098 }
60099 }
60100 }
60101 // When an 'infer T' declaration is immediately contained in a rest parameter declaration, a rest type
60102 // or a named rest tuple element, we infer an 'unknown[]' constraint.
60103 else if (grandParent.kind === 164 /* SyntaxKind.Parameter */ && grandParent.dotDotDotToken ||
60104 grandParent.kind === 186 /* SyntaxKind.RestType */ ||
60105 grandParent.kind === 197 /* SyntaxKind.NamedTupleMember */ && grandParent.dotDotDotToken) {
60106 inferences = ts.append(inferences, createArrayType(unknownType));
60107 }
60108 // When an 'infer T' declaration is immediately contained in a string template type, we infer a 'string'
60109 // constraint.
60110 else if (grandParent.kind === 199 /* SyntaxKind.TemplateLiteralTypeSpan */) {
60111 inferences = ts.append(inferences, stringType);
60112 }
60113 // When an 'infer T' declaration is in the constraint position of a mapped type, we infer a 'keyof any'
60114 // constraint.
60115 else if (grandParent.kind === 163 /* SyntaxKind.TypeParameter */ && grandParent.parent.kind === 195 /* SyntaxKind.MappedType */) {
60116 inferences = ts.append(inferences, keyofConstraintType);
60117 }
60118 // When an 'infer T' declaration is the template of a mapped type, and that mapped type is the extends
60119 // clause of a conditional whose check type is also a mapped type, give it a constraint equal to the template
60120 // of the check type's mapped type
60121 else if (grandParent.kind === 195 /* SyntaxKind.MappedType */ && grandParent.type &&
60122 ts.skipParentheses(grandParent.type) === declaration.parent && grandParent.parent.kind === 189 /* SyntaxKind.ConditionalType */ &&
60123 grandParent.parent.extendsType === grandParent && grandParent.parent.checkType.kind === 195 /* SyntaxKind.MappedType */ &&

Callers 2

typeToTypeNodeHelperFunction · 0.85

Calls 13

createTypeMapperFunction · 0.85
instantiateTypeFunction · 0.85
createArrayTypeFunction · 0.85
getTypeFromTypeNodeFunction · 0.85
makeUnaryTypeMapperFunction · 0.85
getSymbolOfNodeFunction · 0.85
getIntersectionTypeFunction · 0.85
indexOfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…