How to add subdirectory module aliases in tsconfig?

3k views Asked by At

I can set module aliases with no issue:

"baseUrl": "./app",
 "paths": {
   "assets/*": ["assets/*"],
   "components/*": ["components/*"],
   ...
 },           

This works. However, I (actually my client, who wants to extract some parts of the project as a module in the future) want to create subdirectories as aliases to different paths (assume that all the directories as values exist, and I cannot change the actual directory structure):

"paths": {
   "assets/*": ["assets/*"],
   "components/*": ["components/*"],
   "@myapp/api/*": ["services/myapp/*"],
   "@myapp/state/*": ["state/*"],
 },       

Putting aside whether this is a good pattern/practice or not (as I'm asked to do it this way), is this technically possible without touching the physical directory structure (and without the use of 3rd party dependencies)? (I'm using Typescript 3.4.3 and Vscode 1.33.1)

1

There are 1 answers

0
Tomasz Iz On BEST ANSWER
"baseUrl": "./",
"paths": {
  "@myapp/environment": [ "./src/environments/environment" ],
  "@myapp/extensions": [ "./src/extensions" ],
  "@myapp/testing": [ "./src/testing" ],
  "@myapp/constants": [ "./src/app/shared/constants" ],
  "@myapp/data": [ "./src/app/data" ],
  "@myapp/shared": [ "./src/app/shared" ],
  "@myapp/core/*": [ "./src/app/core/*" ],
  "@myapp/*": [ "./src/app/*" ]
}

this works like a charm on my project