-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
JSDoc ...rest parameter syntax cannot support tuple types #49801
Comments
Might be worth mentioning, this is kind of the opposite problem from #43072. |
TS' jsdoc doesn't currently have a good way to match any rest parameter to a /**
* @param {...Parameters<CBFunction>} args
*/
function f(...args) Is like writing this: function f(...args: Parameters<CBFunction>[]) even if it weren't used, everything inside We need an alternate proposal. One possibility is to add the ... on the parameter name: /**
* @param {Parameters<CBFunction>} ...args
*/
function f(...args) This might conflict with existing jsdoc, but I don't think it does off the top of my head. And I think it would be possible to enforce the usual rules on |
That sounds like it could work. What's the process for moving this forward? As far as I can tell, there is no standards body for JSDoc, and the core JSDoc project here on GitHub hasn't pushed a meaningful update since 2017. Do you just kind of discuss the change internally, then go for it? Do you talk to other "flavors" of JSDoc (like Closure) to try to avoid conflicts or incompatibilities? |
The next step is to add a proposal to this issue and maybe a prototype to see what kind of implementation issues arise. The other jsdoc processors don't change much and mostly have a different focus. For adding a new parameter syntax, I'd check to see whether it conflicts with the jsdoc tool or closure, and perhaps tsdoc tooβor whether any of those already have an alternative. |
OK, I'm up for some of that legwork. JSDocAlready checked, no alternative. I can't find an online compiler to test the proposed syntax, but I ran this through the 3.6.9 CLI and got no warnings or errors: /**
* @param {string} a
* @param {boolean} b
*/
export function g(a, b){
console.log(a,b);
}
/**
* @param {Parameters.<g>} ...args - all params
*/
export function f(...args){
console.log(args);
} The doc output just treats TSDocThey do not have a rest-param syntax and nobody has mentioned it in their issues. I found this test site and tried /**
* @param {[number, string]} ...args - all params
*/ but I guess TSDoc doesn't support redundant JSDoc types anyway, because it dumped out
ClosureLooks like it still doesn't support tuples at all which I think would be a prerequisite. That said, if this online compiler I found is up to date, Closure would error on your proposed syntax (
TL;DR: I don't think |
An error is fine -- I want to avoid re-using existing syntax because in Closure's case it makes using TS for the compiler frontend and Closure for the compiler backend (via tsickle) needlessly complex. And in TSDoc's case it would force them to somehow disambiguate between TSDoc syntax and TS syntax. I would recommend looking at the parameter-matching code in the checker and coming up with some tricky examples that show that Some things to think about:
/**
* Does this make any sense? Should it be allowed at all?
* People might want to document individual tuple properties!
* @param {Object} ...args
* @param {number} ...args.0 - should the ... be required/allowed here?
* @param {string} ...args.1 - how do you even write the name for tuple properties?
*/
|
I'm going to need some time to process most of that but wanted to point out that |
Well, now I'm not even sure this feature is required. I thought that // @ts-check
/**
* @callback CBFunction
* @param {number} n
* @param {string} s
*/
/**
* @param {Parameters<CBFunction>} x
*/
function f(...x) {
}
f(12,'b') The only difference from the original example in the issue is that the param type is |
Full disclosure: I filed this issue because a library I use frequently builds their type defs from JSDoc, using /**
* @param {any} arguments - arguments to pass to a callback
*/
Foo.prototype.raiseEvent = function() {
// ...
this.callback.apply(this.scope, arguments);
} If I were writing the code myself, I'd use a named rest-param, rather than |
Well, good news and bad news. It turns out the library in question updated their /**
* @param {Parameters<CBFunction>} args- arguments to pass to a callback
*/
Foo.prototype.raiseEvent = function(...args) {
// ...
this.callback.apply(this.scope, args);
} into The question then becomes, is the official position of the team is that |
Wait, won't I'd vote to change tsd-jsdoc to work they way TS does. |
I ran the JSDoc CLI against ETA: as for |
I don't know anything about tsd-jsdoc, but wouldn't it have to? Doesn't tsd-jsdoc use the Typescript compiler to emit types? Otherwise how does it emit the correct types for all the constructs supported in JS? /**
* @template T
* @param {T} t
*/
function C(t) { this.t = t }
C.prototype.m = function () { return this.t }
// should generate
class C<T> {
m(): T
} or const o = {
a: 1
}
o.C = class {
}
// should generate
declare const o: { a: number }
declare namespace o {
export class C { }
} A lot of JS types don't have associated JSDoc because they can be inferred from values. |
|
Bug Report
π Search Terms
jsdoc rest tuple
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
See Playground link: on
.d.ts
tab, the output isdeclare function f(...args: [number, string][]): void;
, an array-of-tuples. Generally, JSDoc syntax treats{...X} args
asargs: X[]
.π Expected behavior
TS-flavored JSDoc should be able to express any type that TS can express.
The text was updated successfully, but these errors were encountered: