考虑一个具有以下目录 struct 的简单TypScript项目:
| package.json
| tsconfig.json
|
\---src
| app.ts
|
\---foobar
Foo.ts
Bar.ts
tsconfig.json
已配置为./src/
为baseUrl
.
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./dist/",
"baseUrl": "./src/"
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
现在假设我们想在Bar.ts
中输入Foo
.我的理解是,通过设置baseUrl
,我们现在可以使用绝对路径来导入模块
import { Foo } from 'foobar/Foo'
相对于相对路径
import { Foo } from './Foo'
如果我的理解是正确的,typescript编译器应该能够在编译Bar.ts
时自动解析foobar/Foo
到./Foo
.
import { Foo } from 'foobar/Foo';
export class Bar {
foo: Foo;
constructor(num: number) {
this.foo = new Foo(num);
}
}
运行tsc
次编译,没有错误.然而,当我们实际查看编译后的Bar.js
时,我们会发现路径没有正确解析,如果我们运行它,这将给我们一个Cannot find module错误.
"use strict";
const Foo_1 = require("foobar/Foo");
class Bar {
constructor(num) {
this.foo = new Foo_1.Foo(num);
}
}
exports.Bar = Bar;
So my question is: How can I get 100 to correctly resolve the absolute paths when importing modules using 101? Or if this is not something that can be done, what is the purpose of 101 then?