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 cast to const does not work in variable-like initializers #45463

Closed
rbuckton opened this issue Aug 15, 2021 · 0 comments Β· Fixed by #45464
Closed

JSDoc Type cast to const does not work in variable-like initializers #45463

rbuckton opened this issue Aug 15, 2021 · 0 comments Β· Fixed by #45464
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@rbuckton
Copy link
Member

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.

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Aug 17, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants