Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly invalid warning with dual esm/cjs types #55

Closed
atomiks opened this issue Jul 27, 2023 · 2 comments
Closed

Possibly invalid warning with dual esm/cjs types #55

atomiks opened this issue Jul 27, 2023 · 2 comments

Comments

@atomiks
Copy link

atomiks commented Jul 27, 2023

I may be wrong and this is necessary, but I've seen the second setup in a popular open source library - is it invalid or not?

"All good!"

  "types": "./src/types.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./src/types.d.mts",
        "default": "./dist/file.mjs"
      },
      "module": {
        "types": "./src/types.d.ts",
        "default": "./dist/file.esm.js"
      },
      "default": {
        "types": "./src/types.d.ts",
        "default": "./dist/file.umd.js"
      }
    }
  },

Warning

  1. pkg.exports["."].types types is an invalid format when resolving with the "import" condition. Consider splitting out two types conditions for import and require, and use the .mts extension, e.g. pkg.exports["."].import.types: "./src/types.d.mts"
  "types": "./src/types.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": "./src/types.d.ts",
      "import": {
        "types": "./src/types.d.mts",
        "default": "./dist/file.mjs"
      },
      "module": "./dist/file.esm.js",
      "default": "./dist/file.umd.js"
    }
  },
@bluwy
Copy link
Owner

bluwy commented Jul 27, 2023

Yeah it is invalid. When TypeScript tries to resolve the types for import 'my-lib', it will first see "types": "./src/types.d.ts" and use that as it matches all the criteria. It won't use the "types" in "import" (Exports conditions are order-sensitive)

A fix is to move "types": "./src/types.d.ts" after the "import" condition, so the "types" within the "import" condition gets the chance to match first.

Usually for types specific linting, https://arethetypeswrong.github.io is more robust too which you can check against.

@atomiks
Copy link
Author

atomiks commented Jul 27, 2023

Ah ok, thanks for the info!

@atomiks atomiks closed this as completed Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants