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

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

主站蜘蛛池模板: 南岸区| 民勤县| 江油市| 元谋县| 深圳市| 浑源县| 读书| 广安市| 安徽省| 灵武市| 福州市| 林芝县| 长兴县| 奉节县| 密云县| 海门市| 岳西县| 苍梧县| 同德县| 恩平市| 酉阳| 丹东市| 盐城市| 五大连池市| 五华县| 清苑县| 新巴尔虎左旗| 筠连县| 汶川县| 武穴市| 福建省| 津南区| 巴楚县| 洛阳市| 康平县| 大荔县| 吴川市| 阿拉善左旗| 游戏| 临西县| 灵璧县|