microsoft / microsoft/TypeScript

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

Open
#56,002 0 comments 1 reaction 1 assignee View on GitHub

@sandersn is already working on this.

Since Oct 6, 2023.

Bug Domain: Declaration Emit
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Reported at https://github.com/microsoft/TypeScript/issues/46011#issuecomment-1748749050

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;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.