Skip to content

Commit

Permalink
fix: enable compatibility with node16/nodenext module resolution
Browse files Browse the repository at this point in the history
TypeScript's `node16` and `nodenext` module resolution algorithms attempt to adhere more closely to the behavior of the `exports` field.  To that end, if an `exports` field is present in a package, the `types` field is ignored; instead, a `types` conditional export is expected.

Without this change, any consumer using one of these newer--_officially recommended_--module resolution algorithms will be unable to load any `types.d.ts` file from any package.  We retain the `types` field because of backwards compatibility with the legacy `node` module resolution algorithm.

### Should `types.d.ts` exist at all?

_It depends._  While this file is not _truly_ needed for type support if type declarations are shipped alongside each `.js` source file, it's awkward to export any types created via `@typedef` without it.

For instance, say we have a function `foo(opts: FooOpts)`.  `foo` lives in `a.js`, and `FooOpts` is a `@typedef` in `b.js`. The parameter `opts` is tagged as `@param {import('./b.js').FooOpts} opts`, as it should.  Entry point `index.js` re-exports `foo()` from `a.js`.  However, the definition for `FooOpts` _will not be available to consumers_--which will cause compilation errors.  We must, then, re-export this type from the entry point.  To avoid this, we have precious few tools at our disposal:

1. Create a reference of `FooOpts` in `index.js` via `@typedef`, which will export the `FooOpts` type.  If `FooOpts` is generic, this becomes more of a headache, as we have to _copy_ the generics from the original.  This is a hazard, because the generics could become out-of-sync.
2. Create a `.d.ts` (it could also be a `.ts`, which we can assume will generate the associated `.d.ts`; this doesn't matter for our purposes) which re-exports `FooOpts` from `b.js`.  This file will be the titular `types.d.ts`.

The question we need to ask, then, is "do we need to export any `@typedef`s?" If the answer is _yes_, then a `types.d.ts` makes sense.

I didn't actually check to see if `types.d.ts` is appropriate in all of these packages; I just fixed the problem I was encountering.  For `@endo/compartment-mapper`, some work I'm doing will mean re-exporting some `@typedef`s, even if that's not happening currently.  Furthermore, I ignored the packages with `"types": null`, since I don't understand what that's supposed to mean.
  • Loading branch information
boneskull authored and naugtur committed Nov 30, 2023
1 parent e1c63bf commit 9063c47
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/compartment-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"main": "./index.js",
"types": "./types.d.ts",
"exports": {
".": "./index.js",
".": {
"types": "./types.d.ts",
"default": "./index.js"
},
"./import.js": "./import.js",
"./archive.js": "./archive.js",
"./import-archive.js": "./import-archive.js",
Expand Down
5 changes: 4 additions & 1 deletion packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"module": "./index.js",
"types": "./types.d.ts",
"exports": {
".": "./index.js",
".": {
"types": "./types.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion packages/lp32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"module": "./index.js",
"types": "./types.d.ts",
"exports": {
".": "./index.js",
".": {
"types": "./types.d.ts",
"default": "./index.js"
},
"./reader.js": "./reader.js",
"./writer.js": "./writer.js",
"./package.json": "./package.json"
Expand Down
2 changes: 2 additions & 0 deletions packages/ses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"types": "./types.d.ts",
"exports": {
".": {
"types": "./types.d.ts",
"import": "./index.js",
"require": "./dist/ses.cjs",
"types": "./types.d.ts"
},
"./lockdown": {
"types": "./types.d.ts",
"import": "./index.js",
"require": "./dist/ses.cjs",
"types": "./types.d.ts"
Expand Down
5 changes: 4 additions & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"module": "./index.js",
"types": "./types.d.ts",
"exports": {
".": "./index.js",
".": {
"types": "./types.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion packages/where/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"module": "./index.js",
"types": "./types.d.ts",
"exports": {
".": "./index.js",
".": {
"types": "./types.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"scripts": {
Expand Down

0 comments on commit 9063c47

Please sign in to comment.