Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Suggest completions for type arguments of expressions #61758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make getTypeArgumentConstraint work for type arguments of expressions
Previously, `getTypeArgumentConstraint` could only find constraints for
type arguments of generic type instantiations. Now it additionally
supports type arguments of the following expression kinds:

- function calls
- `new` expressions
- tagged templates
- JSX expressions
- decorators
- instantiation expressions
  • Loading branch information
mkantor committed Jun 2, 2025
commit 04bc7b4a209d3b8c2a8c3a914c9d9c3875e40c2d
104 changes: 98 additions & 6 deletions 104 src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42449,6 +42449,48 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}

function getSignaturesFromCallLike(node: CallLikeExpression): readonly Signature[] {
switch (node.kind) {
case SyntaxKind.CallExpression:
case SyntaxKind.Decorator:
return getSignaturesOfType(
getTypeOfExpression(node.expression),
SignatureKind.Call,
);
case SyntaxKind.NewExpression:
return getSignaturesOfType(
getTypeOfExpression(node.expression),
SignatureKind.Construct,
);
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.JsxOpeningElement:
if (isJsxIntrinsicTagName(node.tagName)) return [];
return getSignaturesOfType(
getTypeOfExpression(node.tagName),
SignatureKind.Call,
);
case SyntaxKind.TaggedTemplateExpression:
return getSignaturesOfType(
getTypeOfExpression(node.tag),
SignatureKind.Call,
);
case SyntaxKind.BinaryExpression:
case SyntaxKind.JsxOpeningFragment:
return [];
}
}

function getTypeParameterConstraintForPositionAcrossSignatures(signatures: readonly Signature[], position: number) {
const relevantTypeParameterConstraints = flatMap(signatures, signature => {
const relevantTypeParameter = signature.typeParameters?.[position];
if (relevantTypeParameter === undefined) return [];
const relevantConstraint = getConstraintOfTypeParameter(relevantTypeParameter);
if (relevantConstraint === undefined) return [];
return [relevantConstraint];
});
return getUnionType(relevantTypeParameterConstraints);
}

function checkTypeReferenceNode(node: TypeReferenceNode | ExpressionWithTypeArguments) {
checkGrammarTypeArguments(node, node.typeArguments);
if (node.kind === SyntaxKind.TypeReference && !isInJSFile(node) && !isInJSDoc(node) && node.typeArguments && node.typeName.end !== node.typeArguments.pos) {
Expand Down Expand Up @@ -42487,12 +42529,62 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getTypeArgumentConstraint(node: TypeNode): Type | undefined {
const typeReferenceNode = tryCast(node.parent, isTypeReferenceType);
if (!typeReferenceNode) return undefined;
const typeParameters = getTypeParametersForTypeReferenceOrImport(typeReferenceNode);
if (!typeParameters) return undefined;
const constraint = getConstraintOfTypeParameter(typeParameters[typeReferenceNode.typeArguments!.indexOf(node)]);
return constraint && instantiateType(constraint, createTypeMapper(typeParameters, getEffectiveTypeArguments(typeReferenceNode, typeParameters)));
let typeArgumentPosition;
if (
"typeArguments" in node.parent && // eslint-disable-line local/no-in-operator
Array.isArray(node.parent.typeArguments)
) {
typeArgumentPosition = node.parent.typeArguments.indexOf(node);
}

if (typeArgumentPosition !== undefined) {
// The node could be a type argument of a call, a `new` expression, a decorator, an
// instantiation expression, or a generic type instantiation.

if (isCallLikeExpression(node.parent)) {
return getTypeParameterConstraintForPositionAcrossSignatures(
getSignaturesFromCallLike(node.parent),
typeArgumentPosition,
);
}

if (isDecorator(node.parent.parent)) {
return getTypeParameterConstraintForPositionAcrossSignatures(
getSignaturesFromCallLike(node.parent.parent),
typeArgumentPosition,
);
}

if (isExpressionWithTypeArguments(node.parent) && isExpressionStatement(node.parent.parent)) {
const uninstantiatedType = checkExpression(node.parent.expression);

const callConstraint = getTypeParameterConstraintForPositionAcrossSignatures(
getSignaturesOfType(uninstantiatedType, SignatureKind.Call),
typeArgumentPosition,
);
const constructConstraint = getTypeParameterConstraintForPositionAcrossSignatures(
getSignaturesOfType(uninstantiatedType, SignatureKind.Construct),
typeArgumentPosition,
);

// An instantiation expression instantiates both call and construct signatures, so
// if both exist type arguments must be assignable to both constraints.
if (constructConstraint.flags & TypeFlags.Never) return callConstraint;
if (callConstraint.flags & TypeFlags.Never) return constructConstraint;
return getIntersectionType([callConstraint, constructConstraint]);
}

if (isTypeReferenceType(node.parent)) {
const typeParameters = getTypeParametersForTypeReferenceOrImport(node.parent);
if (!typeParameters) return undefined;
const relevantTypeParameter = typeParameters[typeArgumentPosition];
const constraint = getConstraintOfTypeParameter(relevantTypeParameter);
return constraint && instantiateType(
constraint,
createTypeMapper(typeParameters, getEffectiveTypeArguments(node.parent, typeParameters)),
);
}
}
}

function checkTypeQuery(node: TypeQueryNode) {
Expand Down
6 changes: 3 additions & 3 deletions 6 src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ import {
isTypeOnlyImportDeclaration,
isTypeOnlyImportOrExportDeclaration,
isTypeParameterDeclaration,
isTypeReferenceType,
isValidTypeOnlyAliasUseSite,
isVariableDeclaration,
isVariableLike,
Expand Down Expand Up @@ -5767,8 +5766,9 @@ function tryGetTypeLiteralNode(node: Node): TypeLiteralNode | undefined {
function getConstraintOfTypeArgumentProperty(node: Node, checker: TypeChecker): Type | undefined {
if (!node) return undefined;

if (isTypeNode(node) && isTypeReferenceType(node.parent)) {
return checker.getTypeArgumentConstraint(node);
if (isTypeNode(node)) {
const constraint = checker.getTypeArgumentConstraint(node);
if (constraint) return constraint;
}

const t = getConstraintOfTypeArgumentProperty(node.parent, checker);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////interface Bar {
//// three: boolean;
//// four: {
//// five: unknown;
//// };
////}
////
////function a<T extends Foo>() {}
////a<{/*0*/}>();
////
////var b = () => <T extends Foo>() => {};
////b()<{/*1*/}>();
////
////declare function c<T extends Foo>(): void
////declare function c<T extends Bar>(): void
////c<{/*2*/}>();
////
////function d<T extends Foo, U extends Bar>() {}
////d<{/*3*/}, {/*4*/}>();
////d<Foo, { four: {/*5*/} }>();
////
////(<T extends Foo>() => {})<{/*6*/}>();

verify.completions(
{ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "1", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "2", unsorted: ["one", "two", "three", "four"], isNewIdentifierLocation: true },
{ marker: "3", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "4", unsorted: ["three", "four"], isNewIdentifierLocation: true },
{ marker: "5", unsorted: ["five"], isNewIdentifierLocation: true },
{ marker: "6", unsorted: ["one", "two"], isNewIdentifierLocation: true },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////interface Bar {
//// three: boolean;
//// four: symbol;
////}
////
////class A<T extends Foo> {}
////new A<{/*0*/}>();
////
////class B<T extends Foo, U extends Bar> {}
////new B<{/*1*/}, {/*2*/}>();
////
////declare const C: {
//// new <T extends Foo>(): unknown
//// new <T extends Bar>(): unknown
////}
////new C<{/*3*/}>()
////
////new (class <T extends Foo> {})<{/*4*/}>();

verify.completions(
{ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "1", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "2", unsorted: ["three", "four"], isNewIdentifierLocation: true },
{ marker: "3", unsorted: ["one", "two", "three", "four"], isNewIdentifierLocation: true },
{ marker: "4", unsorted: ["one", "two"], isNewIdentifierLocation: true },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// kind: 'foo';
//// one: string;
////}
////interface Bar {
//// kind: 'bar';
//// two: number;
////}
////
////declare function a<T extends Foo>(): void
////declare function a<T extends Bar>(): void
////a<{ kind: 'bar', /*0*/ }>();
////
////declare function b<T extends Foo>(kind: 'foo'): void
////declare function b<T extends Bar>(kind: 'bar'): void
////b<{/*1*/}>('bar');

// The completion lists are unfortunately not narrowed here (ideally only
// properties of `Bar` would be suggested).
verify.completions(
{ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "1", unsorted: ["kind", "one", "two"], isNewIdentifierLocation: true },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />

// @jsx: preserve
// @filename: a.tsx
////interface Foo {
//// one: string;
//// two: number;
////}
////
////const Component = <T extends Foo>() => <></>;
////
////<Component<{/*0*/}>></Component>;
////<Component<{/*1*/}>/>;

verify.completions(
{ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "1", unsorted: ["one", "two"], isNewIdentifierLocation: true },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////declare function f<T extends Foo>(x: TemplateStringsArray): void;
////f<{/*0*/}>``;

verify.completions({ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////declare function decorator<T extends Foo>(originalMethod: unknown, _context: unknown): never
////
////class {
//// @decorator<{/*0*/}>
//// method() {}
////}

verify.completions({ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////interface Bar {
//// three: boolean;
//// four: {
//// five: unknown;
//// };
////}
////
////(<T extends Foo>() => {})<{/*0*/}>;
////
////(class <T extends Foo>{})<{/*1*/}>;
////
////declare const a: {
//// new <T extends Foo>(): {};
//// <T extends Bar>(): {};
////}
////a<{/*2*/}>;
////
////declare const b: {
//// new <T extends { one: true }>(): {};
//// <T extends { one: false }>(): {};
////}
////b<{/*3*/}>;

verify.completions(
{ marker: "0", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "1", unsorted: ["one", "two"], isNewIdentifierLocation: true },
{ marker: "2", unsorted: ["one", "two", "three", "four"], isNewIdentifierLocation: true },
{ marker: "3", unsorted: [], isNewIdentifierLocation: true },
);
Morty Proxy This is a proxified and sanitized view of the page, visit original site.