Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Omit JSDoc type directives when emitting type declarations with documentation #61664

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

Open
6 tasks done
SethFalco opened this issue May 6, 2025 · 0 comments
Open
6 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@SethFalco
Copy link

πŸ” Search Terms

"omit typedef", "exclude typedef"

βœ… Viability Checklist

⭐ Suggestion

When emitting type declarations from JavaScript sources, the type is already defined in the TypeScript syntax, so there's no need to copy over any @type, @typedef, or @template.

Types can also be stripped out of @param and @returns directives.

This could be taken further, by deleting JSDoc strings altogether if after stripped the types, they become empty.

πŸ“ƒ Motivating Example

For example, I have the following files:

css-select-adapter.js

/**
 * @param {Map<import('../types.js').XastNode, import('../types.js').XastParent>} parents
 * @returns {Required<import('css-select').Options<import('../types.js').XastNode & { children?: any }, import('../types.js').XastElement>>['adapter']}
 */
export function createAdapter(parents) {
  // …
}

tsconfig.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "noEmit": false,
    "declaration": true,
    "emitDeclarationOnly": true,
    "outDir": "types/"
  },
  "include": ["lib/svgo-node.js", "lib/svgo.js"]
}

The emitted type declaration is:

/**
 * @param {Map<import('../types.js').XastNode, import('../types.js').XastParent>} parents
 * @returns {Required<import('css-select').Options<import('../types.js').XastNode & { children?: any }, import('../types.js').XastElement>>['adapter']}
 */
export function createAdapter(parents: Map<import("../types.js").XastNode, import("../types.js").XastParent>): Required<import("css-select").Options<import("../types.js").XastNode & {
    children?: any;
}, import("../types.js").XastElement>>["adapter"];

But the types defined in the JSDoc become unnecessary, it could just be:

/**
 * @param parents
 * @returns
 */
export function createAdapter(parents: Map<import("../types.js").XastNode, import("../types.js").XastParent>): Required<import("css-select").Options<import("../types.js").XastNode & {
    children?: any;
}, import("../types.js").XastElement>>["adapter"];

This could even be taken further, by removing the @param and @returns directives if all of them have no value, and removing the JSDoc outright if after removing empty directives the JSDoc becomes empty:

export function createAdapter(parents: Map<import("../types.js").XastNode, import("../types.js").XastParent>): Required<import("css-select").Options<import("../types.js").XastNode & {
    children?: any;
}, import("../types.js").XastElement>>["adapter"];

πŸ’» Use Cases

In SVGO, we emit type declarations from our JSDocs. However, the type declarations can be quite noisy. It would be valuable if the output was tidied up:

  1. To make them quicker/easier to human-review when needed.
  2. Reduce the size of the published npm package.

We currently do not do any workarounds. We just publish the additional content to npm.

@SethFalco SethFalco changed the title Omit @type and @typedef JSDoc directives with emitting type declarations Omit JSDoc type directives when emitting type declarations with documentation May 6, 2025
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants