Skip to content

Conversation

TrevorBurnham
Copy link

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.

Fixes alexreardon#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.
@joshkel
Copy link

joshkel commented Aug 11, 2025

Thanks for taking a stab at this. I don't think this fix is sufficient. If I run Are the Types Wrong via npx @arethetypeswrong/cli --pack, I see several errors.

It's been my experience that it's very tricky to get a single .d.ts file that will work for both CJS and ESM when default exports are used (because TS + CJS really wants to use that export = syntax, which doesn't work for ESM). I've tried a couple of workarounds for other projects, with mixed success. Hand-writing a separate .d.ts file should work and, considering memoize-one's limited API surface, might be best.

@TrevorBurnham
Copy link
Author

Hmm. I was hoping to just make a small change here; this PR is sufficient to satisfy tsc in the ESM project I was working on. But, I can understand wanting to satisfy @arethetypeswrong/cli. I've spun up a separate PR to do so: #270

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.

TypeScript errors with modules

2 participants