Skip to content

Suggestion: Add support for jsdoc-based templates with constrains #24283

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

Closed
4 tasks done
jantimon opened this issue May 20, 2018 · 3 comments · Fixed by #24600
Closed
4 tasks done

Suggestion: Add support for jsdoc-based templates with constrains #24283

jantimon opened this issue May 20, 2018 · 3 comments · Fixed by #24600
Assignees
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@jantimon
Copy link

jantimon commented May 20, 2018

Search Terms

jsdoc template

Suggestion

This feature request is based on issue #1178.
#1178 led to the implementation of @template and was closed afterwards.

The current implementation does not allow to add constrains for a generic:

add_support_for__template_jsdoc_ issue__1178 _microsoft_typescript

The latest statement from google in 2015 was that they might add it also to the closure-compiler some day:

generic_types_ _google_closure-compiler_wiki

Use Cases

For //ts-check projects it would be possible to use the full power of generics.

Examples

playground_ _typescript

Checklist

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. new expression-level syntax)

Stackoverflow: Is it possible to constrain a generic type defined in a jsDoc @template declaration? (May 2017)

@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2018

I think this should be:

/**
 * @template T {number}
 * @param a {T}
 */
function foo(a) { }

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Salsa labels May 21, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone May 21, 2018
@brendankenny
Copy link
Contributor

brendankenny commented May 21, 2018

Definitely would be helpful. Right now we're using /** @type {...} */ to use bounded generics in js (either defined inline or in an external d.ts file), but that has some serious drawbacks at the moment.

e.g. @type doesn't work on class methods, so we end up assigning the function to the prototype outside of the class body. It's also limited in scope to what's being immediately typed, unlike @template/generics in actual TS, so you can't really apply the type parameters across a whole class, for instance.

Readability is also not a small concern (all crammed into that @type comment :), and being able to declare as regular @params, etc jsdoc tags would be fantastic.

Compatibility with future Closure support isn't super important to us, at least, as we use enough TS-specific features (typeof, indexed access) that we have to do a cleaning pass on the code before passing to Closure anyways. If the solution is reasonable (like the above suggestion), there's also no reason Closure can't adopt it anyways.

@sandersn
Copy link
Member

sandersn commented Jun 4, 2018

#24600 has this change.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 4, 2018
@microsoft microsoft locked and limited conversation to collaborators Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants