Skip to content

Invalid .d.ts generated from JSDoc if module.exports is assigned not an identifierΒ #56229

@remcohaszing

Description

@remcohaszing

πŸ”Ž Search Terms

jsdoc namespace commonjs

πŸ•— Version & Regression Information

  • This changed between versions 3.6 and 3.7 (the first version to support types in JSDoc)

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2&filetype=js#code/PQKhCgAIUgBAXAngBwKYBNUDNIHlnwCWA9gHYDOUMsyATsWrUpAN7ny2GkDmAvpA3hVg4cKAjQ4yAIa1pAW1b4iZcv0EkKw8POLoArgBtUAOlQAPZMSblIAXkgAKDaoCU9gHyteAbnBA

πŸ’» Code

/**
 * @typedef Options
 * @property {string} opt
 */

/**
 * @param {Options} options
 */
module.exports = function loader(options) {}

πŸ™ Actual behavior

TypeScript emits the following .t.ds.

declare function _exports(options: Options): void;
export = _exports;
export type Options = {
    opt: string;
};

πŸ™‚ Expected behavior

I expect the TypeScript to emit the following:

export = loader;
/**
 * @typedef Options
 * @property {string} opt
 */
/**
 * @param {Options} options
 */
declare function loader(options: Options): void;
declare namespace loader {
    export { Options };
}
type Options = {
    opt: string;
};

Or when the expression is unnamed:

export = _exports;
/**
 * @typedef Options
 * @property {string} opt
 */
/**
 * @param {Options} options
 */
declare function _exports(options: Options): void;
declare namespace _exports {
    export { Options };
}
type Options = {
    opt: string;
};

Additional information about the issue

I initially noticed this in the output of https://github.com/mdx-js/mdx/blob/3.0.0/packages/loader/index.cjs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions