feat: Add exports field with .d.mts for TypeScript ESM consumers (nod… - #364
Open
jeffrson wants to merge 1 commit into
Open
feat: Add exports field with .d.mts for TypeScript ESM consumers (nod…#364jeffrson wants to merge 1 commit into
jeffrson wants to merge 1 commit into
Conversation
Author
|
I noticed the project recently switched to Vitest, so the Jest concern that sometimes comes up with ESM migrations no longer applies. A full |
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.
…e16/nodenext)
When using fastest-validator in a TypeScript project with
"type": "module"and"moduleResolution": "node16"or"nodenext", the default import is currently broken:TypeScript determines whether a
.d.tsfile is ESM or CJS based on the file extension and the nearestpackage.json"type"field — not on how it was imported. Since fastest-validator has no"type": "module", TypeScript treatsdist/index.d.tsas CJS even when loaded by an ESM consumer. In CJS interpretation,export default class ValidatormakesValidatora module namespace rather than a constructor.The fix is straightforward: files with a
.d.mtsextension are always treated as ESM by TypeScript, regardless of the package's"type"field. Adding anexportsfield with an"import"condition pointing todist/index.d.mtstells TypeScript to use the ESM-mode types when the package is imported from an ESM context.At runtime this changes nothing — index.js is still the CJS entry point for both
importandrequire. The.d.mtsfile is identical todist/index.d.ts; only the extension differs.CJS consumers are unaffected: the
"require"condition keepsdist/index.d.tsas before.The Deno example in the repo (
import FastestValidator from "https://esm.sh/fastest-validator") actually confirms thatexport default class Validatoris the right declaration for ESM — the type just needs to be served with ESM semantics for TypeScript to pick it up correctly.