repeaterjs / repeater

The missing constructor for creating safe async iterators
https://repeater.js.org
MIT License
459 stars 12 forks source link

TS errors when using "module": "NodeNext" and module type is commonjs #76

Closed jedwards1211 closed 11 months ago

jedwards1211 commented 12 months ago

If I have "module": "nodenext" in my tsconfig and my module type is commonjs (the default) then I get a TS error on import { Repeater } from "@repeaterjs/repeater":

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@repeaterjs/repeater")' call instead.

This is because it resolves to the repeater.d.ts listed in the "types" field of your package.json, but because your package.json declares "type": "module", that repeater.d.ts is ESM.

CJS/ESM is kind of a disaster but fortunately all it takes to fix this is an export map (I'll make a PR):

  "exports": {
    ".": {
      "require": {
        "types": "./cjs/repeater.d.ts",
        "default": "./cjs/repeater.js"
      },
      "types": "./repeater.d.ts",
      "default": "./repeater.js"
    }
  },