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

Skip to content

Support the @private JSDoc directive to exclude private APIs from emitted type declarations #61651

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 4, 2025 · 2 comments
Open
6 tasks done

Comments

@SethFalco
Copy link

SethFalco commented May 4, 2025

πŸ” Search Terms

"private jsdoc"

βœ… Viability Checklist

⭐ Suggestion

Could properties, functions, and classes annotated with the @private JSDoc directive be omitted from the emitted declarations?

tsc should throw an error if a symbol is marked as @private, but is actually imported/exported from another declaration file or used in the signature of another symbol like a function parameter or return type. (And therefore isn't actually an internal API.)

I'm wary this may break some projects if implemented without an opt-in, so it may require an option to enable this behavior, to preserve backward compatibility.

πŸ“ƒ Motivating Example

I maintain a svgo, a library that is written in JavaScript and typed with JSDoc directives, and uses tsc to emit declaration files.

As a library normally has an intended public API, afaik there's no need to distribute type declarations for the internal API.

πŸ’» Use Cases

This is just to reduce the amount of type declarations served to developers if they aren't needed, reducing the size of the package on npm a little.

No workaround is currently needed, assuming the project is a module that uses exports rather than main, as we can limit the public API ourselves. The extra types do not cause any harm, it just makes the final package a little bigger.

For projects that use main instead of exports in their package.json, this may leak more private APIs that end-users may accidently consume and believe to be part of the public interface.

@SethFalco SethFalco changed the title Support the @private JSDoc directive to exclude private APIs from type declarations Support the @private JSDoc directive to exclude private APIs from emitted type declarations May 4, 2025
@xiBread
Copy link

xiBread commented May 4, 2025

If I'm not mistaken, it sounds like you should just enable stripInternal

@SethFalco
Copy link
Author

Thank you for providing advice on the matter! I personally would prefer if I could stick to standard JSDoc directives like @private, but willing to use @internal for now if that can achieve this.

However, I've just tried this but when I add @internal and enable stripInternal: true, the function is not removed from the emitted type declaration files. πŸ€”

I'm using tsc v5.8.3, do you have any idea what could be wrong here? πŸ€”

css-select-adapter.js (snippet from input)

/**
 * @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']}
 * @internal
 */
export function createAdapter(parents) {

tsconfig.build.json (config)

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

css-select-adapter.d.ts (output)

/**
 * @internal
 * @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"];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants