Typescript 为什么tsc命令在用于编译特定文件时不显示编译错误?
我有一个名为app.ts的文件,看起来像这样Typescript 为什么tsc命令在用于编译特定文件时不显示编译错误?,typescript,Typescript,我有一个名为app.ts的文件,看起来像这样 interface Foo { bar:String; } const fn = (foo? :Foo) => foo.bar; app.ts:5:27 - error TS2532: Object is possibly 'undefined'. 5 const fn = (foo? :Foo) => foo.bar; 由于在我的tsconfig.json文件中有strict:true,当我从根文件夹运行tsc
interface Foo {
bar:String;
}
const fn = (foo? :Foo) => foo.bar;
app.ts:5:27 - error TS2532: Object is possibly 'undefined'.
5 const fn = (foo? :Foo) => foo.bar;
由于在我的tsconfig.json文件中有strict:true
,当我从根文件夹运行tsc
时,我得到了一个很好的错误。错误如下所示
interface Foo {
bar:String;
}
const fn = (foo? :Foo) => foo.bar;
app.ts:5:27 - error TS2532: Object is possibly 'undefined'.
5 const fn = (foo? :Foo) => foo.bar;
这正是我所期望的。但是,当我运行
tsc app.ts
时,我没有收到任何错误,并且生成了app.js文件!不是我所期望的。这是为什么?这是因为tsc
可以执行整个项目(并解释tsconfig.json
)或单个文件,但不能同时执行两者
当使用单个文件调用tsc
时,它不会查看tsconfig.json
(请参阅)
在命令行上指定输入文件时,将忽略tsconfig.json文件。
(强调矿山)
然而,这背后的原因对我来说是个谜