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

const assertions in JSDoc #30445

ajafff opened this issue Mar 16, 2019 · 3 comments

const assertions in JSDoc #30445

ajafff opened this issue Mar 16, 2019 · 3 comments


Copy link

@ajafff ajafff commented Mar 16, 2019

Search Terms

const jsdoc


v3.4 added support for as const to mark a literal expression as immutable and disable type widening. This feature uses the TypeScript type assertion syntax {} as const or <const>{}.

In JS files you can use JSDoc to do type assertions: /** @type {T} */(v)
Unfortunately that doesn't work with const assertions.

// @ts-check
const foo = /** @type {const} */([1]); // Error: cannot find name 'const'

This proposes adding support for /** @type {const} */([1]).

An alternative could be /** @const */([1]).

Use Cases

The same as for const assertions in TS files.


My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

This comment has been minimized.

Copy link

@texastoland texastoland commented Apr 18, 2019

It's a small difference, but /** @const */ would be much nicer than /** @type {TypeIOnlyDefinedToMakeThisReadable} */. I see the consistency with /** @type {const} */, too.


This comment has been minimized.

Copy link

@MrEfrem MrEfrem commented Dec 17, 2019

When is planning to implement this?


This comment has been minimized.

Copy link

@grrowl grrowl commented Jan 7, 2020

Not sure if it suits typescript's "value as const" pattern but JSDoc already includes the @constant annotation:

/** @constant
    @type {string}
const RED = 'FF0000';

/** @constant {number} */
var ONE = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.