Skip to content

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

Closed

Description

@jantimon

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)

Activity

  1. mhegazy commented on May 21, 2018

    @mhegazy
    Contributor

    I think this should be:

    /**
     * @template T {number}
     * @param a {T}
     */
    function foo(a) { }
  2. brendankenny commented on May 21, 2018

    @brendankenny
    Contributor

    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.

  3. sandersn commented on Jun 4, 2018

    @sandersn
    Member

    #24600 has this change.

  4. locked and limited conversation to collaborators on Aug 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: JSDocRelates to JSDoc parsing and type generationFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions