microsoft / microsoft/TypeScript

JSDocs don't support properties on functions

Aperta
#56,615 12 commenti 2 reazioni 1 assegnatario Vedi su GitHub

@sandersn ci sta già lavorando.

Dal 1/12/2023.

Bug Domain: JSDoc Experience Enhancement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

JSDoc @property @callback @typedef

✅ Viability Checklist
⭐ Suggestion

Currently JSDoc doesn't support defining properties on functions. This prevents JavaScript users from paralleling Typescript functionality. In particular, it prevents describing flags such as the type property on Redux Action Creators.

To support these, JSDoc would need to support defining function interfaces that include string properties, such as:

/**
 * @typedef {{
 *   (arg: string): { type: string, payload: string };
 *   type: string;
 * }} ActionCreator
 */

/** @type {ActionCreator} */
const createFoo = (arg) => ({ type: createFoo.type, payload: arg });
createFoo.type = 'fooAction';

Ideally we could also extend @callback to include @property values, but this may stretch JSDoc beyond its limits.

📃 Motivating Example

JSDocs are finally in parity with TypeScript, when it comes to adding properties to functions. Previously, if you wanted to add a property to a function, you'd need to import a type from a TypeScript file that looks like this:

type PayloadCreator<Payload> = {
  (payload: Payload): { type: string; payload: Payload };
  type: string;
};

const createNumericPayload: PayloadCreator<number> = (payload) => ({ type: createPayload.type, payload });
createNumericPayload.type = 'numericPayload';

Now you can do it directly in your JavaScript, with

/**
 * @template Payload
 * @typedef {{
 *   (payload: Payload): { type: string; payload: Payload };
 *   type: string;
 * }} PayloadCreator
 */

/** @type {PayloadCreator<number>} */
const createNumericPayload = (payload) => ({ type: createPayload.type, payload });
createNumericPayload.type = 'numericPayload';
💻 Use Cases
  1. What do you want to use this for?
    This allows me to add properties to a function in JavaScript in the same way that I can in TypeScript.
  2. What shortcomings exist with current approaches?
    Currently I must import from TS files, meaning I can't have JS-only environment.
  3. What workarounds are you using in the meantime?
    Using @typedef {import} to pull the definition from a TS file

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.