JSDoc Type cast to const
does not work in variable-like initializers
#45463
Labels
Milestone
const
does not work in variable-like initializers
#45463
Bug Report
π Search Terms
jsdoc cast as const
π Version & Regression Information
as const
in JSDoc since it was added to TypeScript (~v3.5)β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
In a JavaScript file, using a
/** @type {const} */( expr )
cast in the initializer of any variable-like declaration reports a "Cannot find name 'const'" error:In any other position, the cast correctly behaves like a TypeScript
as const
assertion.π Expected behavior
In a JavaScript file, using a
/** @type {const} */( expr )
cast should behave like TypeScript'sas const
in any position, not just random arguments.Workaround:
While
let x = /** @type {const} */({a: 1})
does not work,let x = (/** @type {const} */({a: 1}))
does. This is because our logic to get the effective type annotation node for a variable-like declaration treats the type cast in the initializer as part of the declaration.The text was updated successfully, but these errors were encountered: