Prettier Strips Parens from JSDoc Type Cast #4710
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
Prettier 1.13.5
Playground link
Input:
Output:
Expected behavior:
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 typeNode
, which only has prositional properties and methods. To usesetAttribute
,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 sinceremoveAttribute
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
The text was updated successfully, but these errors were encountered: