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

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.

主站蜘蛛池模板: 清涧县| 垣曲县| 法库县| 武定县| 鄂温| 娱乐| 辽宁省| 扎兰屯市| 都昌县| 南召县| 东兴市| 乐业县| 连平县| 赤壁市| 高密市| 汕尾市| 扶沟县| 余干县| 东台市| 奉化市| 哈巴河县| 柳河县| 辛集市| 罗田县| 阳高县| 重庆市| 丰镇市| 左贡县| 文成县| 宝兴县| 邢台县| 垦利县| 玛曲县| 乌拉特前旗| 于都县| 元阳县| 南雄市| 宜良县| 岫岩| 兴城市| 耒阳市|