FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix getTypeAtLocation for `as const` to not issue a diagnostic by weswigham · Pull Request #36741 · microsoft/TypeScript · GitHub

Fix getTypeAtLocation for as const to not issue a diagnostic - #36741

Merged
Wesley Wigham (weswigham) merged 4 commits into
microsoft:masterfrom
weswigham:fix-ls-gettypeatlocation-for-as-const
Feb 12, 2020
Merged

Fix getTypeAtLocation for as const to not issue a diagnostic#36741
Wesley Wigham (weswigham) merged 4 commits into
microsoft:masterfrom
weswigham:fix-ls-gettypeatlocation-for-as-const

Conversation

Wesley Wigham (weswigham) commented Feb 11, 2020
edited
Loading

Copy link
Copy Markdown
Member

Fixes #34913

checkAssertion in the normal codepath avoids calling into getTypeFromTypeNode on the const in as const expressions, however the checker getTypeAtLocation API may invoke it directly. Previously, this'd cause as resolution error, as we'd resolve an (entirely unused) variable named const and return its' type. With this change, we instead return the type of the expression associated with the const (which should be more useful to API consumers, I hope, and prevents any spurious errors from being added).

Comment thread src/compiler/checker.ts
function getTypeFromTypeReference(node: TypeReferenceType): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
// handle LS queries on the `const` in `x as const` by resolving to the type of `x`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Instead of this shouldn't getTypeOfNode just check if it is const of as const expression should forward it to correct node instead?

Wesley Wigham (weswigham) Feb 11, 2020
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ehhhhhh, it could, but I'd rather push the handling to here, to prevent us from ever attempting to resolve the const in as const as a real type reference, however we may get there in the future (plus, by doing it here the result is cached).

Wesley Wigham (weswigham) merged commit 2b64731 into microsoft:master Feb 12, 2020
Wesley Wigham (weswigham) deleted the fix-ls-gettypeatlocation-for-as-const branch February 12, 2020 21:43

Copy link
Copy Markdown
Member

Is this related to #36490? I believe that PR was also about handling LS requests in larger spans (i.e. not just on identifiers).

Copy link
Copy Markdown
Member Author

Not as far as I know - this is just about fixing us giving a bogus result (and adding a diagnostic) for the type of const in x as const when the LS requests the type at const.

Copy link
Copy Markdown
Member

So it changes from wrong to right, rather than from nothing to something? Makes sense.

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.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using getTypeAtLocation on a const type reference creates a TS2304 diagnostic

4 participants


Back | FazBrowse Home | New Git URL