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

Support for "const modifier on type parameters" with JSDoc #56634

Closed
6 tasks done
leibale opened this issue Dec 1, 2023 · 6 comments Β· Fixed by #56649
Closed
6 tasks done

Support for "const modifier on type parameters" with JSDoc #56634

leibale opened this issue Dec 1, 2023 · 6 comments Β· Fixed by #56649
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@leibale
Copy link

leibale commented Dec 1, 2023

πŸ” Search Terms

"const type parameter jsdoc", "const generic jsdoc", "const template jsdoc"

βœ… Viability Checklist

⭐ Suggestion

v5.0 added support for "const modifier on type parameters", but it is not supported with JSDoc @template tag (or at least I couldn't make it work nor find any documentation about it).

I propose adding support for the const prefix in the @template:

/**
* @template {const Array<any>} T
*/

πŸ“ƒ Motivating Example

The same as "const modifier on type parameters" itself..

πŸ’» Use Cases

  1. What do you want to use this for?
    ...
  2. What shortcomings exist with current approaches?
    ...
  3. What workarounds are you using in the meantime?
    I have to wrap the generic value with /** @type {const} */(...) every time I'm using the function
@leibale leibale changed the title Support for "const modifier on type parameters" in JSDoc Support for "const modifier on type parameters" with JSDoc Dec 1, 2023
@Andarist
Copy link
Contributor

Andarist commented Dec 1, 2023

A workaround for this that you can use at the declaration site:

/** @type {<const T>(a: T) => T} */
function test(a) {
    return a;
}

@leibale
Copy link
Author

leibale commented Dec 1, 2023

@Andarist thanks! :)

@fatcerberus
Copy link

squirrely braces

Well, that’s a new one πŸ˜†

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 1, 2023
@RyanCavanaugh
Copy link
Member

@sandersn this seems like an easy one to just add, can you check?

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.4.0 milestone Dec 1, 2023
@sandersn
Copy link
Member

sandersn commented Dec 1, 2023

The only change is in the parser; parseTemplateTagTypeParameter would need to parse modifiers as well.
The parser produces a normal type parameter, so that should be the only change needed.

The syntax shouldn't be /** @template {const Constraint} T */ though; Typescript syntax puts const before the parameter name, not the constraint name. Better would be /** @template {Constraint} const T */ or /** @template const T, const U */

As before, multiple type parameters in a single tag is ambiguous, so it might be better to forbid modifiers there:

/** @template const T, U */ -- is U const? Who knows.

@fatcerberus
Copy link

fatcerberus commented Dec 1, 2023

is U const? Who knows.

Reminds me of C pointers.

int* p, q;  // p is int*, q is int

Used to trip me up all the time; now it just annoys me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants