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

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.

主站蜘蛛池模板: 岳普湖县| 丰镇市| 蚌埠市| 丰宁| 商洛市| 徐闻县| 新巴尔虎右旗| 安乡县| 济源市| 济南市| 凤山县| 道孚县| 句容市| 定安县| 南部县| 广灵县| 武平县| 东方市| 延长县| 宝鸡市| 石泉县| 达孜县| 黄龙县| 丰原市| 沈阳市| 苗栗市| 海安县| 夏邑县| 赫章县| 天长市| 锡林浩特市| 边坝县| 东兴市| 同仁县| 榕江县| 大足县| 商河县| 马公市| 六枝特区| 漯河市| 无棣县|