#different package.json declared module types for different targets

1 messages · Page 1 of 1 (latest)

short rain
#

Assuming existing module package.json content with conditional exports for the different targets:

  "exports": {
    ".": {
      "types": "./dist/types/index.d.ts",
      "browser": "./dist/browser/index.js",
      "node": "./dist/node/esm/index.js"
    },
    "./package.json": "./package.json"
  }

Is there a way to have the compiler use different "types" depending on the matched export target? I would like to have some specific types differ. I am aware that I can just duplicate the types in the respective targets' directory but I also don't want to bundle the same types multiple times when the ONLY thing that's different from the large module types is one specific type alias declaration.

Alternatively, can I declare type aliases in a .d.ts file so that they depend on something from @types/node but happily ignore it when missing in the users compile environment? @ts-ignore doesn't help here, it will happy silence the .d.ts ignore but the type is then any instead of the types that didn't error out.

I've also tried separating the specific type declaration in its own .d.ts file and guide users to use to compilerOptions.paths resolution overrides depending on their target but I couldn't get that to work either - the file I want to overwrite sits inside the user's node_modules but is required from within it and the paths never matched (used --traceresolution to get the paths it's trying to match to no awail too).

This is a fairly popular module https://www.npmjs.com/package/jose and i'd like the exported KeyLike type alias to be either CryptoKey (from "DOM") or CryptoKey | KeyObject (keyobject from node:crypto) depending on whether the user is compiling for node or for a different target.

hazy veldt
#

Is there a way to have the compiler use different "types" depending on the matched export target?
don't think that's possible with TS directly

#

but if you use a bundler, then there are probably options to specify that