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

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.

主站蜘蛛池模板: 越西县| 佛教| 平江县| 荣昌县| 哈尔滨市| 峨边| 高要市| 扶绥县| 清徐县| 浪卡子县| 中山市| 阿拉善右旗| 桃园市| 洪泽县| 屏东市| 射洪县| 双辽市| 永丰县| 石泉县| 虎林市| 德州市| 雅安市| 阳原县| 射阳县| 江源县| 泰来县| 勐海县| 雷波县| 娄底市| 冷水江市| 临邑县| 大同市| 永登县| 安仁县| 西峡县| 临泉县| 昌黎县| 吉隆县| 闵行区| 晋城| 新津县|