2

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

henriquehbr
  • 1,063
  • 4
  • 20
  • 41
  • Why do you have 2 tsconfig.json ? – Hugo Jan 15 '21 at 20:44
  • Due to what was suggested on [this answer](https://stackoverflow.com/a/61467483/9182121) – henriquehbr Jan 15 '21 at 21:05
  • Not that weird tbh, it's pretty common in monorepo scenarios and besides that, it's a built-in [typescript feature](https://www.typescriptlang.org/docs/handbook/project-references.html), so, it's intended to be used when needed, and no, i'm not adhering to things like reading the `package.json` using node native `fs` module or some ugly hack like that – henriquehbr Jan 16 '21 at 11:34
  • Why do you want to read package.json in your app? – Hugo Jan 17 '21 at 16:26
  • 2
    Display the version field when the `-v` or `--version` flag is passed – henriquehbr Jan 17 '21 at 18:35

1 Answers1

1

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": "../" }
  ]
}
henriquehbr
  • 1,063
  • 4
  • 20
  • 41