microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.58k stars 12.43k forks source link

Attach Documentation to Labeled Tuple Elements #49998

Open harrysolovay opened 2 years ago

harrysolovay commented 2 years ago

Suggestion

I often find myself describing function params as labeled tuples. A simple example:

type AddArgs = [a: number, b: number];
function add(...[a, b]: AddArgs): number {
  return a + b;
}

Unfortunately, this approach prevents us from attaching tsdocs to the params. The following does not work :/

type AddArgs = [a: number, b: number];
/**
 * @param a the first number
 * @param b the second number
 */
function add(...[a, b]: AddArgs): number {
  return a + b;
}

I propose that one be able to attach tsdocs to labeled tuple elements, just as one does to object keys.

type AddArgs = [
  /** the first number */
  a: number,
  /** the second number */
  b: number,
];

I also propose that––when spread in place of function params––these tsdocs are treated as if they were the result of @param annotations on the function declaration.

🔍 Search Terms

Documentation, labeled, tuple, elements, tsdocs, signature

✅ Viability Checklist

My suggestion meets these guidelines:

harrysolovay commented 2 years ago

Linking to #41165 for posterity

sirius16 commented 1 year ago

I hope this gets done, it would be very helpful in writing overload functions were vargs/args would be a union of labeled tuples.