Skip to content

fix(types): resolve the package under nodenext for both ESM and CJS - #296

Open
nikolas-sapa wants to merge 1 commit into
mbret:mainfrom
nikolas-sapa:fix/271-nodenext-types
Open

nikolas-sapa wants to merge 1 commit into
mbret:mainfrom
nikolas-sapa:fix/271-nodenext-types

Conversation

@nikolas-sapa

Copy link
Copy Markdown

Fixes #271

What was wrong

Under moduleResolution: "nodenext" (or node16) the package's types were unusable:

  • ESM — dist/index.d.ts re-exported through extensionless paths and directories (export * from './lib/binding/useObserve'), which nodenext refuses to follow, so every export looked missing (TS2305).
  • CJS — the require condition points at ./dist/index.cjs, so TypeScript looks for ./dist/index.d.cts. There was none, and with exports present the top-level types field is not read (TS7016).

Fix

  • Declarations are now bundled into one self-contained dist/index.d.ts with no relative imports, via vite-plugin-dts's bundleTypes (api-extractor under the hood).
  • Each exports condition gets its own types: index.d.ts for import, index.d.cts for require (emitted by copying the bundled declarations in a small closeBundle step — the file has no relative imports, so the copy is valid for both).
  • @microsoft/api-extractor is added as a dev dependency. Its typescriptCompilerFolder is pointed at @typescript/old (the JS compiler, already pulled in by @typescript/typescript6) because the typescript package here is the native port and ships no lib.*.d.ts files api-extractor can follow — without this it fails with Unable to follow symbol for "Promise".

A side effect of bundling: npm pack now ships 8 files instead of 113 — the test/playground declarations from #283 (main.d.ts, tests/, *.test.d.ts) are gone.

Verified

  • npm run check, npm run build, npm run test:ci (318 tests) — all green.
  • Packed tarball tested in a consumer project with { module: "nodenext", moduleResolution: "nodenext", strict: true, noEmit: true }: import { useObserve } from "reactjrx" now compiles from both esm.mts and cjs.cts (previously TS2305 / TS7016 + TS2834, reproduced against the pre-fix build).
  • moduleResolution: "bundler" consumers unaffected — still resolves.
  • npx @arethetypeswrong/cli --pack . reports no problems for node10, node16 CJS, node16 ESM, and bundler.

Nothing breaks: deep reactjrx/dist/* imports were already unreachable through the exports map.

Bundle the emitted declarations into one self-contained index.d.ts so
nothing references sibling files without an extension, and give each
exports condition its own types file: index.d.ts next to index.js for
import, index.d.cts next to index.cjs for require.

api-extractor needs a JS TypeScript compiler to follow lib symbols, so
it is pointed at @typescript/old; the typescript package here is the
native port and ships no lib files it can read.

Fixes mbret#271
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

Successfully merging this pull request may close these issues.

Types: Node projects with nodenext resolution see no exports, and CommonJS ones get no types

1 participant