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

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.

主站蜘蛛池模板: 天全县| 宁远县| 礼泉县| 云龙县| 绿春县| 灌阳县| 定结县| 麟游县| 大英县| 浦东新区| 五峰| 富顺县| 宝丰县| 衡南县| 青冈县| 察雅县| 清流县| 三河市| 保山市| 高碑店市| 杭锦旗| 治县。| 玉龙| 利辛县| 墨脱县| 景谷| 常州市| 同江市| 雷州市| 武威市| 方正县| 商水县| 永嘉县| 天柱县| 大丰市| 方城县| 浦县| 龙山县| 通海县| 明光市| 罗城|