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

Prettier Strips Parens from JSDoc Type Cast #4710

Closed
rbiggs opened this issue Jun 17, 2018 · 1 comment
Closed

Prettier Strips Parens from JSDoc Type Cast #4710

rbiggs opened this issue Jun 17, 2018 · 1 comment
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:duplicate Issues that are a duplicate of a previous issue

Comments

@rbiggs
Copy link

rbiggs commented Jun 17, 2018

Prettier 1.13.5
Playground link

--parser babylon

Input:

/** @type {HTMLElement} */ (element).removeAttribute(prop)

Output:

/** @type {HTMLElement} */ element.removeAttribute(prop);

Expected behavior:

/** @type {HTMLElement} */ (element).removeAttribute(prop)

JSDoc supports type casting by using an inline comment with the type and by enclosing the target element in parens. Prettier is stripping those parens out, causing the type cast to fail. In the above JavaScript, element is of type Node, which only has prositional properties and methods. To use setAttribute, removeAttribute and other Element features, you need to type cast the element to type HTMLElement. This is done with the inline comment and by enclosing the object to cast in parens. Removing those parens causes the type checker to flag the element as an error since removeAttribute does not exist on type Node.

You can read more about JSDoc type casting here (scroll down to the bottom of the page): https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler
Or here in the Visual Studio Code documentation: https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript. Scroll down to the last item in the supported features, right before Patterns that are known NOT to be supported

@ikatyang
Copy link
Member

Hi, this looks like a duplicate of #4287 and should be fixed by #4648.

Prettier pr-4648
Playground link

--parser babylon

Input:

/** @type {HTMLElement} */ (element).removeAttribute(prop)

Output:

/** @type {HTMLElement} */ (element).removeAttribute(prop);

@ikatyang ikatyang marked this as a duplicate of #4287 Jun 17, 2018
@ikatyang ikatyang added type:duplicate Issues that are a duplicate of a previous issue lang:javascript Issues affecting JS labels Jun 17, 2018
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Sep 15, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Sep 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:duplicate Issues that are a duplicate of a previous issue
Projects
None yet
Development

No branches or pull requests

2 participants