Skip to content

JS declaration emit invalid when typedefs present and module.exports is assigned a non-identifier #56002

Open
@andrewbranch

Description

@andrewbranch

Reported at #46011 (comment)

Repro steps

Run declaration emit on this JS

/** @typedef {string} S */

/** @type {any} */
module.exports = "";

Expected behavior

declare const _exports: any;
export = _exports;
declare namespace _exports {
    export { S };
}
type S = string;

The declaration file is error-free.

Actual behavior

declare const _exports: any;
export = _exports;
export type S = string;

The declaration file errors with An export assignment cannot be used in a module with other exported elements.

Additional info

Declaration emit works as expected if module.exports is assigned to an identifier that doesn’t necessitate the _exports temp variable synthesis:

/** @typedef {string} S */

/** @type {any} */
const x = "";
module.exports = x;

produces:

export = x;
/** @typedef {string} S */
/** @type {any} */
declare const x: any;
declare namespace x {
    export { S };
}
type S = string;

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions