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.
When a project that uses pleasantest uses TS with
moduleResolution: nodenext
, TS becomes unhappy with pleasantest because it interprets the .d.ts as a non-ESM file, even though it was imported as an ESM.This is all a big headache. I'm going to do my best to explain it but I might be a little bit wrong:
If a
.ts
file is authored as a module (e.g. it has import statements), it may be either compiled to commonjs or compiled to a module.If a file with import statements is compiled to commonjs, there is potentially a problem where that file may have had
import 'some-esm-file'
which would get transpiled torequire('some-esm-file')
. But, importing an ESM file from a commonjs file is not allowed. So even though the file was authored in ESM (with import statements), since it was transpiled to commonjs, it would no longer be valid.To fix this real problem (ignoring typescript), pleasantest has an export map in
package.json
, so that a user canrequire('pleasantest')
orimport 'pleasantest'
. This export map says "when you require me, load this CJS file, when you import me, load this ESM file".This all works fine at runtime but TS doesn't know about it and thinks there is still a problem. When I create a project with these TS settings:
then I get the following error:
This TS error is not helpful because at runtime everything is fine (the export map handles all the CJS/ESM interop as necessary).
To work around this, I added separate
types
fields for each of the parts of the export map, pointing to.d.cts
and.d.mts
files.types.d.cts
,types.d.mts
, andtypes.d.ts
all have exactly the same contents.I wish we'd gotten ES modules sooner so that we wouldn't have ever had to deal with pretending that import and export were syntax sugar for require and module.exports. It causes so many headaches now.