Support completions contextual types in more places#20768
Merged
2 commits merged intomasterfrom Jan 3, 2018
Merged
Conversation
a71011d to
374654b
Compare
374654b to
7526d65
Compare
sandersn
requested changes
Jan 3, 2018
Member
sandersn
left a comment
There was a problem hiding this comment.
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?
| // 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) { |
Member
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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; |
Member
There was a problem hiding this comment.
is this really not used anywhere else besides services?
src/services/completions.ts
Outdated
| case ts.SyntaxKind.EqualsToken: | ||
| return ts.isVariableDeclaration(parent) | ||
| ? checker.getContextualType(parent.initializer) | ||
| : ts.isBinaryExpression(parent) |
Member
There was a problem hiding this comment.
can you reformat like so?
return ts.isVariableDeclaration(parent) ? checker.getContextualType(parent.initializer) :
ts.isBinaryExpression(parent) ? checker.getTypeAtLocation(parent.left) : undefined;
sandersn
approved these changes
Jan 3, 2018
This pull request was closed.
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #20760
Sequel to #20020 that supports completions-specific contextual types on an identifier after a
newexpression. Moves this code from checker to a wrapper incompletions.tssince 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 }.)