Search code examples
typescriptaliaspackage.jsontsconfigtsc

TypeScript path aliases stopped working with project references


Previously, i was facing a issue about package.json not being under rootDir, but then, i could find a question on StackOverflow addressing the exact same issue, after following the steps suggested by this answer i ended up having my path aliases unrecognized by tsc when trying to generate declaration files by running tsc --build src

Notice: I didn't included declaration related properties on tsconfig.json like "declaration": true or "emitDeclarationOnly": true because i couldn't even transpile the code at first, and i'm focused on getting path aliases to work as they seem to be a more complex and separated problem from .d.ts generation, if this come to be a issue, i include later in a comment on this same issue

File structure:

.
├── src/
│   ├── helpers/
│   │   └── parseOptions.ts
│   ├── eswatch.ts
│   └── tsconfig.json
├── package.json
└── tsconfig.json

./tsconfig.json

{
  "compilerOptions": {
    "rootDir": ".",
    "outDir": ".",
    "resolveJsonModule": true,
    "composite": true,
  },
  "files": ["package.json"],
}

./src/tsconfig.json

{
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "../types",
    "resolveJsonModule": true
  },
  "paths": {
    "@eswatch/*": ["./*"]
  },
  "references": [
    { "path": "../" }
  ]
}

Path aliases are not being recognized at all when used in conjunction to project references, indeally, they should work as normal, and consequently, the declaration files should be emitted


Solution

  • Solved it! and turns out it was a really dumb mistake that somehow went unnoticed, the paths property of tsconfig.json should be inside compilerOptions instead of being at object root level, the following works for me:

    {
      "compilerOptions": {
        "rootDir": ".",
        "outDir": "../types",
        "resolveJsonModule": true,
        "paths": {
           "@eswatch/*": ["./*"]
         }
      },
      "references": [
        { "path": "../" }
      ]
    }