What is the difference between tslints “typedef” and typescript compilers “noImplicitAny”?
我正在使用tslint整理typescript代码。我已经搜索了一段时间,但找不到使用typescript编译器选项
1 2 3 4 5 6 7 | "no-inferrable-types": [true] "typedef": [ true, "property-declaration", "variable-declaration", // ... etc ... ] |
存在差异并且差异很大。
NoImplicite any会在将变量识别为
1 2 3 4 5 | let arr = [] arr.forEach(item => item) // Variable 'arr' implicitly has an 'any[]' type.(7005) // but [1,2,3].map(item => item) // OK |
在第二种情况下,未声明项类型(如第一种情况),但是TS编译器知道什么是
调用
也这样的代码:
1 2 | let arr = [] arr.forEach((item: any) => item) // OK |
不会引发错误。
在linter规则的情况下,它们只是强制您添加类型定义,因此此代码