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

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.

主站蜘蛛池模板: 宜都市| 卓尼县| 灵武市| 金寨县| 朝阳市| 库伦旗| 东光县| 嘉峪关市| 昭觉县| 蕉岭县| 高要市| 昂仁县| 汝州市| 新泰市| 宜兰县| 武穴市| 宜兰县| 淮北市| 岑溪市| 无锡市| 武冈市| 志丹县| 邵武市| 重庆市| 江口县| 唐山市| 射阳县| 栾川县| 贞丰县| 南溪县| 富宁县| 炉霍县| 大关县| 同仁县| 富顺县| 江安县| 林周县| 贵定县| 斗六市| 玛曲县| 衡山县|