Repository navigation
fix: ship type declarations for the CommonJS require entry - #1011
Open
pcriadoperez wants to merge 4 commits into
Open
pcriadoperez wants to merge 4 commits into
pcriadoperez wants to merge 4 commits into
Conversation
Adds tests/package-types.sh, which packs the package, installs it into a fresh consumer project and runs tsc under --module nodenext against a CommonJS (.cts) and an ES module (.mts) consumer. The CJS cases currently fail with TS7016 because the `require` export has no declarations (ccxt#1010). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XpTLXUxpfhtzV8YPuQiJAh
Under --module node16/nodenext TypeScript resolves types through
`exports` and ignores the top-level `typings` field, so CommonJS consumers
got TS7016 because `exports["."].require` had no declarations.
- Add `types` conditions to both the `import` and `require` exports.
- Generate dist/cjs/node-binance-api.d.cts at build time. The CJS bundle
does `module.exports = Binance`, so the declaration uses `export =` and
re-exposes the ESM type exports via a merged namespace (Binance.Order,
or `import type { Order } from 'node-binance-api'`). The type list is
read from the emitted ESM declarations so it stays in sync with src/.
- Add `bundle-cjs-types` to the build and a `package-types-test` script.
Fixes ccxt#1010
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XpTLXUxpfhtzV8YPuQiJAh
…Interop The package types test only ran under --module nodenext with the locked TypeScript 5.8, where a CommonJS declaration may import an ES module declaration. Run it under node16 as well, and add a pass with esModuleInterop disabled. Both currently fail (TS1541 and TS1259). Also make the script fail fast (set -euo pipefail) and stop hiding npm install errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XpTLXUxpfhtzV8YPuQiJAh
The generated dist/cjs/node-binance-api.d.cts imported the ESM declarations, which TypeScript rejects from a CommonJS file under --module node16/node18, and under nodenext before 5.8 (TS1541/TS1479) when skipLibCheck is off. - Copy dist/*.d.ts to dist/cjs/types/*.d.cts, rewriting relative imports to the .cjs copies and package default imports (`import WebSocket from 'ws'`) to `import X = require(...)` so they also work with esModuleInterop disabled. - Type-check the generated declarations at the end of the generator (node16 and nodenext, esModuleInterop on and off, skipLibCheck off), so the build fails instead of shipping broken types. - Don't treat type-only re-exports of classes as value exports. - Run the package types test right after the build in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XpTLXUxpfhtzV8YPuQiJAh
pcriadoperez
marked this pull request as ready for review
November 16, 2026 13:20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1010
Problem
Under
--module node16/nodenext, TypeScript resolves types throughexportsand ignores the top-leveltypingsfield.exports["."].requirepointed only atdist/cjs/node-binance-api.cjs, with no declarations next to it. CommonJS consumers (a.ctsfile, or a.tsfile in a package without"type": "module") therefore gotTS7016and an implicitany.Fix
package.json:importandrequireeach get atypescondition.scripts/generate-cjs-types.mjs, run asbundle-cjs-typesinnpm run build:dist/*.d.tstodist/cjs/types/*.d.cts..cjscopies.import WebSocket from 'ws') becomeimport WebSocket = require('ws').node16/node18, and undernodenextbefore 5.8 (TS1541/TS1479).dist/cjs/node-binance-api.d.cts, which usesexport =because the rollup CJS bundle doesmodule.exports = Binance.export defaultdeclaration would type the module asrequire(...).default, which isundefinedat runtime.Order,Interval,Dictionary<T>, …) are re-exposed through a namespace merged with the class, so bothBinance.Orderandimport type { Order } from 'node-binance-api'work..d.tswith the TypeScript compiler API, so new types insrc/are picked up without edits.skipLibCheckoff under node16 and nodenext, withesModuleInteropon and off. If the output doesn't check, the build fails rather than shipping broken types.Generated entry (excerpt):
Tests (TDD)
Each fix commit comes after a commit that adds the failing test for it.
tests/package-types.sh(npm run package-types-test, run in CI right after Build):tscwithskipLibCheckoff;esModuleInteropboth on and off;cjs-require.cts:import Binance = require('node-binance-api'), plus namespaced typescjs-default-import.cts: default import andimport type { Interval }from a CJS fileesm-import.mts: regression guard for the ESM entry@ts-expect-errorguards in each file make the test fail if the module silently resolves toany.masterthe test fails withTS7016.esModuleInterop: false(TS1259). The test now covers both, and both pass.@types/*ignored:esModuleInteropon and off.esModuleInteropcan't be turned off in TS 7.lib.domvs@types/nodeconflict, which doesn't involve this package.require()returns the class and the ESM default import is the class, matching the types.Notes
typingsfield is unchanged. Undernode10resolution,import X = require('node-binance-api')still sees the ESMexport defaulttypes, as onmaster. That is out of scope here.🤖 Generated with Claude Code
https://claude.ai/code/session_01XpTLXUxpfhtzV8YPuQiJAh