Skip to content

Revisit TypeScript definitions for dual cjs/esm #1880

@shadowspawn

Description

@shadowspawn

I think we got this technically wrong in #1704. Basically, we need a separate type definition file for each of cjs and esm. This is a common mistake and in our defence the documentation may have got clearer in the last year!

From https://www.typescriptlang.org/docs/handbook/esm-node.html

It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct.

Looking back, @ChocolateLoverRaj did make a second file when investigating, and just reexporting the cjs types is hopefully sufficient like in comment: #1703 (comment)

The TypeScript gurus actually suggest not having a "types" entry and just having type definition sitting beside the entry point. However, we have historically had the definitions tucked away in a folder and I'm ok with continuing with that.

Related links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions