TypeScript 路径别名停止使用项目引用
TypeScript path aliases stopped working with project references
之前,我遇到了 package.json
不在 rootDir
之下的问题,但是后来,我找到了一个 question on Whosebug addressing the exact same issue, after following the steps suggested by this answer 我的路径别名最终无法被 tsc
当试图通过 运行 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
文件结构:
.
├── 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": "../" }
]
}
路径别名在与项目引用结合使用时根本无法识别,实际上,它们应该正常工作,因此,应该发出声明文件
解决了!事实证明这是一个非常愚蠢的错误,不知何故被忽视了,tsconfig.json
的 paths
属性 应该在 compilerOptions
内而不是在对象根级别,以下工作对我来说:
{
"compilerOptions": {
"rootDir": ".",
"outDir": "../types",
"resolveJsonModule": true,
"paths": {
"@eswatch/*": ["./*"]
}
},
"references": [
{ "path": "../" }
]
}
之前,我遇到了 package.json
不在 rootDir
之下的问题,但是后来,我找到了一个 question on Whosebug addressing the exact same issue, after following the steps suggested by this answer 我的路径别名最终无法被 tsc
当试图通过 运行 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
文件结构:
.
├── 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": "../" }
]
}
路径别名在与项目引用结合使用时根本无法识别,实际上,它们应该正常工作,因此,应该发出声明文件
解决了!事实证明这是一个非常愚蠢的错误,不知何故被忽视了,tsconfig.json
的 paths
属性 应该在 compilerOptions
内而不是在对象根级别,以下工作对我来说:
{
"compilerOptions": {
"rootDir": ".",
"outDir": "../types",
"resolveJsonModule": true,
"paths": {
"@eswatch/*": ["./*"]
}
},
"references": [
{ "path": "../" }
]
}