Skip to content

fix: serve correct CJS type declarations from package.json exports - #21

Merged
dogmar merged 1 commit into
mainfrom
fix/m4-cjs-types
Jun 15, 2026
Merged

fix: serve correct CJS type declarations from package.json exports#21
dogmar merged 1 commit into
mainfrom
fix/m4-cjs-types

Conversation

@dogmar

@dogmar dogmar commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Audit medium M4.

Problem

Every exports entry (and the top-level types) pointed at the ESM .d.mts declaration — including under the "require" condition. So a TS node16/nodenext consumer doing require("@klinking/colander") (or a subpath) resolved ESM type declarations for a CJS import — the classic attw "incorrect types for require" failure. The build already emits .d.cts for the index and all 7 format subpaths; they were simply unreachable.

Fix

  • Nest types under import/require per subpath: .d.mts for import, .d.cts for require, with default for the JS.
  • Point the top-level types at .d.cts to match main (which is the CJS index.cjs).
  • Add typesVersions mapping each subpath to its .d.cts so legacy moduleResolution: node (node10) — which can't read exports subpaths — also resolves subpath types. (This axis was already failing before; the flat map never supported it either.)
  • Expose ./package.json.

Verification

@arethetypeswrong/cli --pack"No problems found 🌟", all green across node10, node16 (from CJS), node16 (from ESM), and bundler (was 💀 for node16-from-CJS on every entry, and 💀 for node10 subpaths). vp run ready: lint+typecheck 0/0, all suites green.

🤖 Generated with Claude Code

Every exports entry pointed `types` at the ESM `.d.mts` even under the
`require` condition, so TS `node16`/`nodenext` CJS consumers resolved
ESM declarations for a CJS `require` — the classic attw "incorrect types
for require" failure. The build already emits `.d.cts` for the index and
all 7 subpaths; they were just unreachable.

Nest `types` under `import`/`require` per subpath (`.d.mts` for import,
`.d.cts` for require), point the top-level `types` at `.d.cts` to match
`main` (CJS), add `typesVersions` so legacy node10 resolves subpath types,
and expose `./package.json`.

Verified with @arethetypeswrong/cli: "No problems found" across node10,
node16 (CJS + ESM), and bundler.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dogmar
dogmar merged commit 1a3aa61 into main Jun 15, 2026
6 checks passed
@dogmar
dogmar deleted the fix/m4-cjs-types branch June 15, 2026 17:39
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant