allotment icon indicating copy to clipboard operation
allotment copied to clipboard

TypeScript type exports are not working for modern environments

Open spaguette opened this issue 2 years ago • 2 comments

Hi!

Issue description

When importing anything from allotment, my tsc gives me the following error:

Could not find a declaration file for module 'allotment'. '<(Project path omitted)>/node_modules/allotment/dist/modern.mjs' implicitly has an 'any' type.
  There are types at '<(Project path omitted)>/node_modules/allotment/dist/types/src/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'allotment' library may need to update its package.json or typings.ts(7016)

Possible context

This seems to be related to the issue reported in TS: https://github.com/microsoft/TypeScript/issues/52363

Environment

allotment 1.19.0 typescript 5.1.6 vite 4.4.4 NodeJS 18.13.0 (Mac M1)

I'm using "module": "esnext", "moduleResolution": "bundler", in my tsconfig. No other library so far has given the same error for me.

Possible workaround

While it is a hack, the workaround that worked for me so far was to create a typings file like the following:

declare module "allotment" {
  export * from "allotment/dist/types/src/index.d.ts";
}

spaguette avatar Jul 19 '23 10:07 spaguette

This tripped me up too. Thanks for the workaround!

For additional context/evidence, here's the arethetypeswrong page for the latest allotment version: https://arethetypeswrong.github.io/?p=allotment%401.19.2

mark-dr avatar Jul 26 '23 11:07 mark-dr

Hello, I encountered this issue in version 1.19.3 on vite

could you please add the following content to package.json 'exports':

{
  "exports": {
    ".": {
      "require": "./dist/modern.mjs",
      "import": "./dist/module.js",
      "types": "./dist/types/src/index.d.ts"
    }
  },
}

here is doc for node about this fields: https://nodejs.org/api/packages.html#packages_conditional_exports

pangxiaoli avatar Oct 16 '23 03:10 pangxiaoli