官术网_书友最值得收藏!

--noImplicitAny

This forces us to explicitly specify the any type where we want to use it. This forces us to think about our use of any and whether we really need it.

Let's explore this with an example:

  1. Let's add a doSomething method to our OrderDetail class that has a parameter called input with no type annotation:
export class OrderDetail {
...
doSomething(input) {
input.something();
return input.result;
}
}
  1. Let's do a compilation with the --noImplicitAny flag in the Terminal:
tsc orderDetail --noImplicitAny

The compiler outputs the following error message because we haven't explicitly said what type the input parameter is:

orderDetail.ts(14,15): error TS7006: Parameter 'input' implicitly has an 'any' type.
  1. We can fix this by adding a type annotation with any or, better still, something more specific:
doSomething(input: {something: () => void, result: string}) {
input.something();
return input.result;
}

If we do a compilation with --noImplicitAny again, the compiler is happy.

主站蜘蛛池模板: 德州市| 张家口市| 图木舒克市| 云林县| 德令哈市| 伽师县| 金堂县| 怀宁县| 兴安盟| 商丘市| 鸡西市| 汾西县| 灵石县| 洛浦县| 莱西市| 荥经县| 阿合奇县| 共和县| 鲁甸县| 荃湾区| 呼和浩特市| 吉首市| 新疆| 江达县| 凤城市| 苍梧县| 噶尔县| 宝清县| 泌阳县| 贞丰县| 林周县| 泸州市| 通海县| 晋州市| 宝应县| 新巴尔虎右旗| 长寿区| 建始县| 博爱县| 亳州市| 北宁市|