From a7a0c545942f0df33ddac32e6df92e4de13ce24d Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Mon, 11 Aug 2025 17:46:37 -0400 Subject: [PATCH] adding ESM type declarations Fixes #267 When consumers use `"type": "module"` with `"module": "node16"` and `"moduleResolution": "node16"`, the `export default` declaration in `memoize-one.d.ts` is considered invalid because those types are interpreted strictly as CommonJS. The solution here is to create a separate type declaration file called `memoize-one.d.mts` with the same contents. The `.mts` extension is the key here, since it allows the `export default` to be interpreted correctly as ESM. I've also added an `exports` declaration to the `package.json` to point ESM-aware consumers to the correct types. Since all existing code and types are the same, this change should be fully backward-compatible. --- .prettierignore | 3 ++- package.json | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.prettierignore b/.prettierignore index 6fd10a4..85cb8a2 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ -/node_modules/* \ No newline at end of file +/node_modules/* +/dist diff --git a/package.json b/package.json index e57b6d2..109e468 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,18 @@ "main": "dist/memoize-one.cjs.js", "types": "dist/memoize-one.d.ts", "module": "dist/memoize-one.esm.js", + "exports": { + ".": { + "import": { + "types": "./dist/memoize-one.d.mts", + "default": "./dist/memoize-one.esm.js" + }, + "require": { + "types": "./dist/memoize-one.d.ts", + "default": "./dist/memoize-one.cjs.js" + } + } + }, "sideEffects": false, "files": [ "/dist", @@ -96,7 +108,7 @@ "build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:flow", "build:clean": "yarn rimraf dist", "build:dist": "yarn rollup -c", - "build:typescript": "yarn tsc ./src/memoize-one.ts --emitDeclarationOnly --declaration --outDir ./dist", + "build:typescript": "yarn tsc ./src/memoize-one.ts --emitDeclarationOnly --declaration --outDir ./dist && cp ./dist/memoize-one.d.ts ./dist/memoize-one.d.mts", "build:flow": "cp src/memoize-one.js.flow dist/memoize-one.cjs.js.flow", "perf": "yarn ts-node ./benchmarks/shallow-equal.ts", "perf:library-comparison": "yarn build && node ./benchmarks/library-comparison.js",