Search code examples
angulartypescripttsconfig

Angular/Typescript tsconfig.json configure path alias with multiple paths


I configured some path alias in my tsconfig.json and they works fine:

"paths": {
  "@app/models": ["src/app/app.models"],
  "@app/routing": ["src/app/app-routing.module"],
  "@app/routing/*": ["src/app/routing/*"]
}

What I need now is to configure another path alias with multiple paths. Should be something similar to this:

"@app/ab": ["src/app/test-a/a", "src/app/test-b/b"]

The problem is when I import from @app/ab because I can see only classes declared in src/app/test-a/a. Is there a possibile solution to achieve this?


Solution

  • You could make an 'index.ts' file where you export "src/app/test-a/a", "src/app/test-b/b". And then refer to the index file.

    // src/app/test-index.ts

    export * from 'src/app/test-a/a';
    export * from 'src/app/test-b/b';
    
    

    // tsconfig.json

    ...
    "@app/ab": ["src/app/test-index.ts"]
    ...
    

    OR

    Add the folders instead of the files

    "@app/ab/*": ["src/app/test-a/*", "src/app/test-b/*"]
    

    But then everything within 'test-a' and 'test-b' will also be exposed via that path.