-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
default types for @template generics in JSDoc #29401
Comments
Well, I like the color of your bikeshed. You know, as long as it doesn't end up making anything explode :-). |
In the case of functions, typescript could also infer the default value of a generic parameter from the default value of the parameter it is bounded to. Example : /**
* @template T
* @param {string} key
* @param {T} [defaultValue=null]
* @return {number[] | T}
*/
function getAssociatedValue(key, defaultValue = null) {
// ...
}
const test1 = getAssociatedValue("foo");
const test2 = getAssociatedValue("bar", [42]); Here I would expect Typescript to infer that the type of Currently (version 3.6.4), typescript return an error on the parameter
|
As jsdoc checking already supports the extends syntax (https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html#template), i,e, this is valid: /**
* @template {any} T
* @param {string} key
* @param {T} [defaultValue]
*/
function getAssociatedValue(key, defaultValue) { It'd be better to just extend that and add support for an /**
* @template {any = null} T
* @param {string} key
* @param {T} [defaultValue]
*/
function getAssociatedValue(key, defaultValue) { Matches nicely to Equivalent ts: function getAssociatedValue<T extends any = null>(key: string, defaultValue?: T) |
The OP's "bikeshedding" does not give a syntax for a type parameter that is both constrained and includes a default value. I would submit that it probably makes sense to support Has there been any opinion from the team about this? (I see @weswigham is the only contributor participating?) I just linked to this issue from |
A problem with that is the We must find a way to do this without breaking JSDoc syntax. |
@trusktr that's another argument in favor of a /**
* @param {number} [arg=1]
*/
/**
* @template {MyBase} T=MySubclass
*/ Though, to be fair, |
Is there any agreement on a way forward, to the extent that a PR would be welcome? This is a pretty severe blocker for me, since I express all my types in JSDoc, and want to add template parameters in a backward-compatible way (especially to take an existing type and turn it into a template without breaking callers). My only other option that I'd prefer not to, is to move the (many interdependent) declarations into a |
I want to chime in as well, I've been using JSDoc as a way to add compile safety to my plain JS code, and it's been a mostly pleasant experience, until I hit this limitation. I second the following syntax, as it preserves the current behavior and mirrors default type assignments in TypeScript: /**
* @template {MyBase} T=MySubclass
*/ |
why not just keep jsdoc syntax ? /**
* @template T {keyof typeof $Plugin['Plugins']}
* @param {T} _pluginName
* @param {typeof $Plugin['Plugins'][T]} pluginOptions
*
*/ we need a support for this :) |
|
I'm loosing my mind, the lack of this feature is making stuff impossibly hard. |
Very very bad.... Need update generated types manually 😞 |
doesn't #45483 resolve this? |
Yep |
Yes, I'll close this now. |
Search Terms
template default types, template default jsdoc, generic default jsdoc
Suggestion
In pure TypeScript we can have default values for generic parameters e.g.:
However there doesn't seem to be a way to represent this in JSDoc, TypeScript seems to just split
@template
on comma and take the longest identifier.This proposal is to extend
@template
in JSDoc to support generic defaults (e.g.Priority=number
).Bikeshed syntax
The syntax isn't really important, but the capability would be helpful. I don't think this would cause any grammar issues but I'm not sure.
Use Cases
Same as within TypeScript, just extended to JSDoc.
Example with bikeshed syntax
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: