在TypeScript中从另一个子项目导入模块的正确方法是什么?
我有这个项目结构:
myproject/
tsconfig.json
tsconfig-base.json
src/
tsconfig.json
core/
A.ts
B.ts
extra/
C.ts
D.ts
test/
tsconfig.json
test.ts
built/
core/...
extra/...
test/...
tsconfig-base.json:
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"declaration": true,
"outDir": "built",
"composite": true,
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
tsconfig.json:
{
"files": [],
"includes": [],
"references": [
{ "path" : "./src" },
{ "path" : "./test" }
]
}
src / tsconfig.json:
{
"include": ["**/*"],
"extends": "../tsconfig-base.json",
"compilerOptions": {
"rootDir": "."
}
}
test / tsconfig.json:
{
"extends" : "../tsconfig-base.json",
"include" : ["*"],
"references": [
{ "path": "../src" }
],
"compilerOptions" : {
"outDir": "../built/test",
"rootDir": "."
}
}
在test/test.ts
中,我这样做:
import { Thing } from "../core/A"
因为this thread建议在使用outDir
时应“从结束的任何地方”导入模块,所以我希望上面的方法能起作用(因为built/core/A.js
通过指定的路径相对于built/test/test.js
)。类似地,TypeScript docs on project references显示了具有类似结构的示例,其中import
如上所述省略了“ src”。
相反,我得到:
test/test.ts:1:28 - error TS2307: Cannot find module '../core/A'.
1 import { Thing } from "../core/A"
为什么?使用项目引用时,TypeScript如何搜索导入? (请注意,在这种情况下,被迫在所有导入路径中都包括“ src/
”是不合理的。)>
容纳上述项目结构的正确方法是什么?
谢谢!
从TypeScript的另一个子项目导入模块的正确方法是什么?我有这个项目结构:myproject / tsconfig.json tsconfig-base.json src / tsconfig.json core / ...
通过更新文件src / tsconfig.json: