Current TypeScript packages setup and improvement suggestion #2051
Closed
Description
While working on my previous PR, I delved into the TypeScript packages setup in this repository. Here's my understanding, along with some issues I've found and a suggestion for improvement.
I'm not fully aware of the reasoning behind the current setup, so please correct me if I'm wrong or if I'm missing something.
Current package setup
- Pure ESM package.
exports
field points to compiled JS (.js
) files.types
ortypesVersions
field points to source TS (.ts
) files.- Does not generate DTS (TS type declaration,
.d.ts
) files. moduleResolution
intsconfig.json
is set tonode
, an alias ofnode10
.
This setup seems designed to provide a good developer experience within this repository. Developers/tsc directly refer to the TS files of dependent packages, while Node.js or bundlers use the JS files.
Issues with this setup for MUD packages users
- Users need to include the
@types/*
dependencies, which are MUD packages' dev dependencies, in their projects to pass theirtsc
, even if they do not directly use these dependencies. This is because the packages expose TS files instead of DTS files, leading to projects'tsc
treating them as regular in-project TS files. This setup may also increase thetsc
time of the projects. - Users must use specific configs in their
tsconfig.json
, particularlymoduleResolution: node10
. The recommended modern settings,bundler
ornode16
, are not usable because these settings rely onexports
fields rather thantype
/typesVersions
fields for finding types, leading to errors due to missing type files.
Suggestion for improvement
The goal is to:
- Maintain the current developer experience in this repository.
- Avoid affecting existing (
moduleResolution: node10
) users. - Resolve the
@types/*
dependencies issue. - Allow users to opt for
bundler
ornode16
in their projects.
Steps to improve:
- Generate DTS files (set
dts: true
intsup.config.ts
)- This would enable users to use
bundler
ornode16
, which find their corresponding DTS files based onexports
. These users wouldn't need to have the@types/*
. - No impact on developers and
node10
users, asnode10
does not recognizeexports
. - The potential downside is increased build time for DTS generation.
- This would enable users to use
- Remove the
@types/*
necessity fornode10
users (Optional)- This can be achieved by overriding
types
/typesVersions
fields to point to DTS instead of TS on packing, using pnpm'spublishConfig
feature. - No impact on developers since the only change is the addition of
publishConfig
fields in packages'package.json
.
- This can be achieved by overriding
- Switch to
bundler
ornode16
fromnode10
in this repository's packages if desired (Optional)- The packages'
package.json
should haveexports
fields pointing to TS files andpublishConfig.exports
fields pointing to JS files.
- The packages'
Note
- These changes have not been tested yet. I wanted to confirm my understanding of the current setup and know the desired future direction. (It seems like step 1 is working without major issues in my local
/templates
project withbundler
andnode16
.) - Even when using MUD externally with a JS + DTS setting, I believe it's still possible to maintain the "go to TS definition" experience for users development environments by utilizing
tsc --declarationMap
.
Metadata
Assignees
Labels
No labels