Improve support for internal packages by resolving path aliases #58657
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
π Search Terms
Internal packages
Path alias
Resolve paths
β Viability Checklist
β Suggestion
Add a
tsconfig.json
to theexports
field inpackage.json
so that internal packages can share their TypeScript config especially their path aliases.π Motivating Example
Please find a reproduction example here: https://github.com/zirkelc/typescript-internal-packages
I'm using internal packages to share code in my monorepo. An internal package is package which is NOT published to NPM and which is NOT explicitly compiled with TypeScript before it can be used by other packages. It exports the source TypeScript
*.ts
files from thepackage.json
via themain
,types
, andexports
fields:This internal package is meant to be installed by other packages in the same repo:
And can be used like a normal dependency which resolve to the original uncompiled TypeScript code:
Everything good so far. This pattern works well as long as none of the internal packages is using TypeScript path aliases. However, let's re-structure the internal package and use path aliases like
~/math
:The path alias
~/math
points to the sub-folder./src/math/index.ts
. The previous./src/index.ts
becomes a simple barrel file exportsNow, the internal package
@repo/libs-math
uses path aliases which are defined its own localtsconfig.json
. However, the previous import in./apps/foo
will now start to fail:TypeScript doesn't find the function anymore, even though go to definition to
@repo/libs-math
still works.I assume the reason is the following: TypeScript resolves the imports starting from the app
./apps/foo/src/index.ts
to the internal package./libs/math/src/index.ts
. However, TypeScript still works inside the./apps/foo/tsconfig.json
and is not aware of the path aliases defined by the./libs/math/tsconfig.json
. It would need a way to recognize it is in a different package now and must look for the righttsconfig.json
in./libs/math
.That's where my idea of exporting the
tsconfig.json
in thepackage.json
comes in. TypeScript would notice it is working in a different package when it was moving from./apps/foo/src/index.ts
to./libs/math/src/index.ts
. Then it sees this new package has a local TypeScript config exported from the local./libs/math/package.json
. It sees the path aliases and applies these aliases for all paths inside./libs/math/*
.π» Use Cases
There are two possible workarounds:
tsconfig.paths.json
which defines all path aliases for the entire project. Each package-leveltsconfig.json
extends from this config. There are two disadvantages to this workaround:@repo/libs-math
via a typescript path alias instead of a node package (workspace) import.tsconfig.json
. The package@repo/apps-foo
has a node dependency to@repo/libs-math
viapackage.json
. For project references, this dependency must be replicated again in thetsconfig.json
viareferences
. So the dependency graph ofpackage.json
must be repeated for every new internal package.The text was updated successfully, but these errors were encountered: