Skip to content

TypeScript resolution fails when trying to import the package from a CJS module #28

@tao-cumplido

Description

@tao-cumplido

Are The Types Wrong lists the issue "Masquerading as ESM" for the regex package.

I guess the issue lies in package exports field, it only lists a single type definition file but an .mjs and a .cjs source for ESM and CJS respectively. Newer TS versions would need two definitions .d.mts and .d.cts. I believe they can generally be identical. There's some edge case import/export syntax only available in .cts files:
https://www.typescriptlang.org/docs/handbook/modules/reference.html#export--and-import--require

I haven't tested it, but I believe rewriting the exports field to something like this could work:

{
  "exports": {
    ".": {
      "import": {
        "types": "./dist/regex.d.mts",
        "default": "./dist/regex.mjs"
      },
      "require": {
        "types": "./dist/regex.d.cts",
        "default": "./dist/regex.cjs"
      }
    }
  }
}

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