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

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

主站蜘蛛池模板: 东乡| 高陵县| 罗源县| 邢台县| 罗平县| 碌曲县| 苏尼特右旗| 循化| 进贤县| 开鲁县| 行唐县| 通江县| 封开县| 峡江县| 绥中县| 东乌| 荆州市| 永春县| 阿巴嘎旗| 固阳县| 宜黄县| 鸡西市| 平罗县| 吉水县| 罗江县| 尚志市| 博罗县| 灵台县| 岢岚县| 汝阳县| 钟山县| 长武县| 水富县| 达州市| 界首市| 青铜峡市| 固阳县| 浑源县| 通辽市| 舟山市| 湖州市|