Skip to content

JSDoc doesnt support generics correctly #56102

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

Open
thepassle opened this issue Oct 13, 2023 · 5 comments
Open

JSDoc doesnt support generics correctly #56102

thepassle opened this issue Oct 13, 2023 · 5 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@thepassle
Copy link

🔎 Search Terms

jsdoc generics

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

Given the following code:

function api<T>(endpoint: string): Promise<T> {
  return fetch(endpoint).then(res => res.json());
}

When I consume the api function in a .js file, I'd still like to leverage the T generic via JSDoc-only. There currently isnt a straightforward way to do this, and the DX of this could definitely be improved.

🙁 Actual behavior

Cant leverage the T generic

🙂 Expected behavior

I can leverage the T generic

Additional information about the issue

This is a long overdue feature :)

@fatcerberus
Copy link

Not directly related to the issue but that function scares me. It's like when people ask for JSON.parse to be generic.

@thepassle
Copy link
Author

Not directly related to the issue but that function scares me. It's like when people ask for JSON.parse to be generic.

Whatever, thats irrelevant, its just an example.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Oct 16, 2023
@thepassle
Copy link
Author

Maybe a potential solution/proposal could be something like this?

function api<T>(endpoint: string): Promise<T> {
  return fetch(endpoint).then(res => res.json());
}

/**
 * @typedef {Object} Foo
 * @property {string} bar
 */

const r = await api/** @type {Foo} */('/api/foo');

// the type of `r` is `Foo`

This is still valid js, and its similar to how the generic would be provided in ts: api<Foo>('/api/foo')

@KonnorRogers
Copy link

This seems similar to this: #27387

But also 👍

using Generic functions with JSDOC is quite painful.

@thepassle
Copy link
Author

@RyanCavanaugh Would it be possible to get some feedback from the team on either this proposal or whats proposed in #27387?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants