Skip to content

fix(types): add typesVersions to support CommonJS imports in TypeScript#301

Open
jbrun-hulo wants to merge 1 commit into
forzagreen:mainfrom
jbrun-hulo:fix/set-types-versions
Open

fix(types): add typesVersions to support CommonJS imports in TypeScript#301
jbrun-hulo wants to merge 1 commit into
forzagreen:mainfrom
jbrun-hulo:fix/set-types-versions

Conversation

@jbrun-hulo
Copy link
Copy Markdown

Pull Request

What does this do?

Add typesVersions field to package.json to map type definitions, enabling proper CommonJS module resolution in TypeScript projects such as NestJS v10.

Without this, TypeScript fails to resolve types when importing n2words via require() or with moduleResolution set to Node.

Related Issue

Checklist

  • Tests pass (npm test)
  • Linting passes (npm run lint)

Adding a new language? (click to expand)

Recommended: Use the scaffolding tool to generate all files automatically:

npm run lang:add -- <code>

Manual setup: If not using the scaffolding tool, ensure you have:

  • Language module in src/<code>.js exporting toCardinal(), toOrdinal(), and/or toCurrency()
  • Test fixture in test/fixtures/<code>.js with matching exports
  • Tests pass (npm test)
Breaking change? (click to expand)

Describe what breaks and how users should update:

Copy link
Copy Markdown
Collaborator

@TylerVigario TylerVigario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed the fix solves a real problem — reproduced the TS2307: Cannot find module 'n2words/en-US' error in a sandbox TS project with moduleResolution: "node" against main, and verified this patch clears it.

One thing worth tightening before merge: the catch-all "*": ["./src/*.d.ts"] mapping also makes n2words/utils/* type-check, even though exports explicitly walls it off with "./utils/*": null. At runtime the import still fails with ERR_PACKAGE_PATH_NOT_EXPORTED, so consumers get phantom types — TS green-lights an import that explodes when the code runs.

Suggested tighter pattern that preserves the existing privacy boundary:

"typesVersions": {
  "*": {
    "utils/*": [],
    "*": ["./src/*.d.ts"]
  }
}

The more-specific utils/* key matches first; an empty array means no candidates, so TS falls through and errors — matching the exports behavior. I verified locally that:

  • n2words/en-US → resolves (catch-all * matches)
  • n2words/utils/parse-cardinal → TS error (matches the runtime block)

@jbrun-hulo jbrun-hulo force-pushed the fix/set-types-versions branch from fb088a6 to 20a24f9 Compare May 21, 2026 06:38
@jbrun-hulo jbrun-hulo requested a review from TylerVigario May 21, 2026 06:38
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.

2 participants