2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is a solution (I used this solution for @app/models alias that have about 20 paths) but to me it looks like a workaround in this specific case. Why should I need to create and extra file for two paths instead of simply add a value in array?
I've updated the answer. Check typescriptlang.org/docs/handbook/module-resolution.html. The way you're trying to use it, the 2nd item in your path is a 'fallback'. It's not for single files, but for folders.
I understand. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.