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

--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.

主站蜘蛛池模板: 陇西县| 沂水县| 汽车| 伊金霍洛旗| 韶关市| 夏河县| 上犹县| 黄龙县| 莆田市| 璧山县| 津南区| 田林县| 澎湖县| 横峰县| 富民县| 成安县| 府谷县| 金溪县| 郧西县| 贵州省| 澎湖县| 班戈县| 双江| 蒙阴县| 大余县| 衢州市| 庆元县| 双鸭山市| 邹城市| 保康县| 淮滨县| 新野县| 巴林左旗| 山东省| 台北县| 千阳县| 宜阳县| 如皋市| 本溪市| 沙湾县| 锦屏县|