Skip to content

Suggestion: Generics and JSDoc #5854

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
kitsonk opened this issue Dec 1, 2015 · 6 comments
Closed

Suggestion: Generics and JSDoc #5854

kitsonk opened this issue Dec 1, 2015 · 6 comments
Assignees
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Dec 1, 2015

Related to both #2913 and #2916, it is great having support for JSDoc annotations. One thing that does make using TypeScript with JSDoc a little bit of a challenge is how to annotate Generics. You can supply a lot of information to a consumer of the API now, but if you have generics, the consumer might be left wondering "what is this generic used for, what should I pass?"

There doesn't seem to be an appropriate tag for it in JSDoc. I am suggesting that maybe @generic would be appropriate, where the generic is named like a param is named and the type annotation would be if the generic extends another type. For example:

/**
 * My generic function
 * @generic {string} T     Give me some generic goodness
 * @param   {T}      value Some sort of generic thing
 * @returns {T}            and I will just it back to you
 */
function foo<T extends string>(value: T): T {
  return value;
}
@weswigham
Copy link
Member

Actually, the closure compiler specifies @template for generics. The closure compiler jsdoc support list is more complete than the use jsdoc site, from a type system perspective.

@kitsonk
Copy link
Contributor Author

kitsonk commented Dec 1, 2015

@weswigham thanks! Ok, then I suggest the following:

/**
 * My generic function
 * @param   {T} value Some sort of generic thing
 * @returns {T}       and I will just it back to you
 * @template    T     Give me some generic goodness
 */
function foo<T extends string>(value: T): T {
  return value;
}

The one limitation is that it @template doesn't seem to have a structure for narrowing the type of generic.

@RyanCavanaugh RyanCavanaugh self-assigned this Dec 1, 2015
@RyanCavanaugh
Copy link
Member

I'm working on JSDoc support now and will keep this in mind

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Dec 1, 2015
@billti
Copy link
Member

billti commented Dec 1, 2015

Note @RyanCavanaugh that the initial work @CyrusNajmabadi did already had support for the JsDoc @template tag (see 0cb45c5 ).

@NoelAbrahams
Copy link

@template already has a tracker #1178

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

indeed. thanks @NoelAbrahams

@mhegazy mhegazy closed this as completed Dec 3, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 3, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
No Sign up for free to subscribe to this conversation on GitHub. Already have an account? No Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants