Bug Report
π Search Terms
jsdoc cast as const
π Version & Regression Information
- We have half-supported
as const in JSDoc since it was added to TypeScript (~v3.5)
β― Playground Link
Playground link with relevant code
π» Code
// @checkJs: true
// @allowJs: true
// @strict: true
let x = /** @type {const} */(1); // error: cannot find name 'const'
x; // any
let y;
y = /** @type {const} */(1); // works
y; // 1
let z = (/** @type {const} */(1)); works
z; // 1
π 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:
- VariableDeclaration
- PropertyDeclaration
- PropertyAssignment
- BindingElement
- ParameterDeclaration
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's as 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.
Bug Report
π Search Terms
jsdoc cast as const
π Version & Regression Information
as constin 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 constassertion.π Expected behavior
In a JavaScript file, using a
/** @type {const} */( expr )cast should behave like TypeScript'sas constin 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.