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

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.

主站蜘蛛池模板: 久治县| 循化| 禹州市| 太湖县| 南江县| 观塘区| 云林县| 深水埗区| 土默特左旗| 肥西县| 兴城市| 温泉县| 五大连池市| 道孚县| 抚州市| 丰台区| 房产| 荣昌县| 元氏县| 绍兴市| 喀喇| 永泰县| 会宁县| 安多县| 苏尼特右旗| 平凉市| 盐城市| 蕲春县| 进贤县| 北宁市| 舟曲县| 怀仁县| 娱乐| 富源县| 噶尔县| 汝阳县| 湟中县| 永泰县| 平凉市| 东光县| 东辽县|