Typescript Path aliases outside rootDir in tsconfig

Viewed 1380

I see this question, but it is slightly different and has no answers

I have a couple of git submodules. They are large existing projects, and I thought I could just reuse pieces directly like:

myproject/
   src/
   submod1/src
   submod2/src

with tsconfig.json like

        <snip...>
        "rootDir": "src/",
        "outDir": "./dist",
        "paths": {
            "@submod1/*": [ "submod1/src/*" ],
            "@submod1/*": [ "submod2/src/*" ],
            "@app/*": [ "src/*" ]
        },

And in my code things like

import { SomeVar } from '@submod1/constants';

But vscode (and tsc presumably) are reporting File '<path>/submod1/src/index.ts' is not under 'rootDir' '<path>/src'. 'rootDir' is expected to contain all source files.

I'm just trying to do something simple and pragmatic. Is there a way to achieve this? I guess I could do something with symlinks, but that seems like a big mistake. I could put the submodules under src/ but that also seems wrong.

1 Answers

Looks like there's no 'clear' solution to this. You can either

  • include the whole myproject in your tsconfig.json's rootDir which might be error prone if you're going to have more modules and wouldn't like to reuse anything besides submod1 and submod2

or

{
  "compilerOptions": {
    "rootDirs": ["src", "submod1/src", "submod2/src"]
  }
}
Related