-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support completions contextual types in more places #20768
Conversation
a71011d
to
374654b
Compare
374654b
to
7526d65
Compare
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.
Seems fine except that I don't understand why removing the contextual typing from ===
and others doesn't change anything in the batch compilation tests. Can you explain?
@@ -13943,7 +13943,7 @@ namespace ts { | |||
// the contextual type of an initializer expression is the type annotation of the containing declaration, if present. | |||
function getContextualTypeForInitializerExpression(node: Expression): Type { | |||
const declaration = <VariableLikeDeclaration>node.parent; | |||
if (hasInitializer(declaration) && node === declaration.initializer || node.kind === SyntaxKind.EqualsToken) { |
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.
Why does =
go away? I guess that it's because in batch compilation, =
doesn't have a type, and node
is in fact only ever =
when called from services.
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.
Right, this code was added in #20020 and this PR moves the special cases to completions.ts
.
case SyntaxKind.ExclamationEqualsEqualsToken: | ||
case SyntaxKind.ExclamationEqualsToken: | ||
// For completions after `x === ` | ||
return node === operatorToken ? getTypeOfExpression(binaryExpression.left) : undefined; |
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.
is this really not used anywhere else besides services?
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.
Yes, this was also added by #20020.
src/services/completions.ts
Outdated
case ts.SyntaxKind.EqualsToken: | ||
return ts.isVariableDeclaration(parent) | ||
? checker.getContextualType(parent.initializer) | ||
: ts.isBinaryExpression(parent) |
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.
can you reformat like so?
return ts.isVariableDeclaration(parent) ? checker.getContextualType(parent.initializer) :
ts.isBinaryExpression(parent) ? checker.getTypeAtLocation(parent.left) : undefined;
Fixes #20760
Sequel to #20020 that supports completions-specific contextual types on an identifier after a
new
expression. Moves this code from checker to a wrapper incompletions.ts
since some of these contextual types don't really make sense outside of completions. (E.g., we contextually type an identifier inconst x: T = new iden|
asT
, whereas a correct contextual type would be{ new(): T }
.)