Skip to content
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

JSDoc type assertions for expression without parens #18212

Closed
mjbvz opened this issue Sep 1, 2017 · 3 comments
Closed

JSDoc type assertions for expression without parens #18212

mjbvz opened this issue Sep 1, 2017 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Sep 1, 2017

From #5158

TypeScript Version: 2.5.2

Code
For the JavaScript:

//@ts-check

const img = /** @type {HTMLImageElement} */ document.getElementById('#cat')
img.src = 'cat.gif'

Expected behavior:
The type of img is HTMLImageElement

Actual behavior:
The type of img is any. You currently need parens around the expression for this to work properly:

//@ts-check

const img = /** @type {HTMLImageElement} */ (document.getElementById('#cat'))
img.src = 'cat.gif'

I feel that the parens should not be required. The JSDoc cast should have the lowest precedence and apply to the entire expression on the right hand side

@mjbvz mjbvz added the VS Code Tracked There is a VS Code equivalent to this issue label Sep 1, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 5, 2017

this is the intended design. looking up these cast expressions in comments is not cheep. moreover, scoping rules are not clear either.
we made a design choice to only look them up for parenthesized expressions.

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug Design Limitation Constraints of the existing architecture prevent this from being fixed and removed VS Code Tracked There is a VS Code equivalent to this issue Working as Intended The behavior described is the intended behavior; this is not a bug labels Sep 5, 2017
@weswigham
Copy link
Member

weswigham commented Sep 12, 2017

The bigger issue, I think, is that we'd need to start parsing and maintaining JSDoc ASTs on all expressions - and we're very frugal about where we do that right now. The actual lookup is fairly cheap - no moreso than a normal TS-style cast, really (only a slightly more complicated AST to traverse to find the type to cast to).

@mjbvz
Copy link
Contributor Author

mjbvz commented Sep 12, 2017

Ok, if there is a technical reason why the parens necessary, feel free to close this out. I only opened this because when I first tried to write a jsdoc cast I forgot the parens around the expression, and it took me a moment to realize this

@mhegazy mhegazy closed this as completed Sep 12, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants