microsoft / TypeScript

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

satisfies support in JSDoc #51086

Closed brendankenny closed 1 year ago

brendankenny commented 2 years ago

Suggestion

πŸ” Search Terms

satisfies, jsdoc

βœ… Viability Checklist

My suggestion meets these guidelines:

⭐ Suggestion

It would be fantastic to have satisfies support in jsdoc. This was previously discussed in the satisfies PR (with the suggestion to open a separate tracking issue) and in the 8/17/2022 Design Meeting.

There was a discussion about using /** @type {satisfies T} */, but I would love to see TS be more opinionated on this and go for /** @satisfies {T} */.

Two main reasons:

The design meeting mentioned:

We try not to add new tags that aren't doc'd in the usejsdoc site. Ship has sailed with TS-specific syntax in JSDoc, right?

But: unknown tags are common in the wild and jsdoc (the utility) has allowUnknownTags set by default, so @satisfies information isn't included in the generated docs, but it also doesn't cause any errors. Parser-wise, it should also be relatively easy to support in the future, if there's a desire.

The syntax ship has definitely sailed, in contrast. TS hasn't added tags before AFAIK, but common TS-specific syntax is already often not parsed and causes errors (e.g. /** @type {{name: string}[]} */ const arr = [];).

πŸ“ƒ Motivating Example

/** @typedef {"red" | "green" | "blue"} Colors */

/**
 * Ensure that we have exactly the keys from 'Colors'.
 * @satisfies {Record<Colors, unknown>}
 */
const favoriteColors = {
    red: 'yes',
    green: false,
    blue: 'kinda',
    platypus: false,
//  ~~~~~~~~ error - "platypus" was never listed in 'Colors'.
}

πŸ’» Use Cases

satisfies in jsdoc!

DanielRosenwasser commented 2 years ago

I think /** @satisfies {Type} */ is my preference as well.

a-tarasyuk commented 2 years ago

/** @satisfies {Type} */

It looks good to me too. However, I'm not sure if adding new (custom?) jsdoc tags is OK.

DanielRosenwasser commented 1 year ago

Thank you @a-tarasyuk!