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

Type inference

We have seen how type annotations are really valuable, but they involve a lot of extra typing. Luckily, TypeScript's powerful type inference system means we don't have to provide annotations all the time. We can use type inference when we immediately set a variable value.

Let's look at an example:

  1. Let's add the following variable assignment in the TypeScript playground:
let flag = false;
  1. If we hover our mouse over the flag variable, we can see that TypeScript has inferred the type as boolean:

  1. If we add another line beneath this, to incorrectly set flag to Table, we get a type error:

So, when we declare a variable and immediately set its type, we can use type inference to save a few keystrokes.

主站蜘蛛池模板: 泸水县| 云龙县| 资兴市| 荆门市| 泰和县| 汽车| 黎平县| 新泰市| 正镶白旗| 富川| 扎赉特旗| 岳阳市| 金山区| 淳化县| 西丰县| 全州县| 合江县| 阿坝| 来安县| 通海县| 鄂伦春自治旗| 永靖县| 木兰县| 南康市| 石狮市| 南川市| 曲麻莱县| 左权县| 新郑市| 班玛县| 扶绥县| 河间市| 宁夏| 喜德县| 广南县| 海南省| 平乐县| 蓝山县| 汶上县| 江城| 龙州县|