-
Notifications
You must be signed in to change notification settings - Fork 13k
Enhance type argument completions #62170
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
mkantor
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
mkantor:type-argument-completions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+401
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
65296fe
Make `getTypeArgumentConstraint` work for type arguments of expressions
mkantor c086eb4
Suggest completions of property values in type arguments
mkantor 2136987
Suggest completions within string literals in type arguments
mkantor d5bbf02
Suggest completions within tuple type arguments
mkantor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42624,6 +42624,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) { | ||
|
@@ -42662,12 +42704,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We typically avoid these kinds of checks; is there no helper that achieves the same thing? Or at least, a switch case? |
||
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) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter10.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
32 changes: 32 additions & 0 deletions
32
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter11.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
25 changes: 25 additions & 0 deletions
25
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter12.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
18 changes: 18 additions & 0 deletions
18
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter13.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter14.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
15 changes: 15 additions & 0 deletions
15
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter15.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
35 changes: 35 additions & 0 deletions
35
tests/cases/fourslash/completionListInTypeLiteralInTypeParameter16.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels suspicious that this is the first time we'd need a func like this; there's nothing else that does this? Not just
getResolvedSignature
?