- Learn React with TypeScript 3
- Carl Rippon
- 125字
- 2021-06-10 19:16:32
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:
- Let's add the following variable assignment in the TypeScript playground:
let flag = false;
- If we hover our mouse over the flag variable, we can see that TypeScript has inferred the type as boolean:
- 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.
推薦閱讀
- OpenStack Cloud Computing Cookbook(Third Edition)
- Learning Python Web Penetration Testing
- Apache Spark 2.x Machine Learning Cookbook
- 深入淺出Spring Boot 2.x
- JS全書:JavaScript Web前端開(kāi)發(fā)指南
- Mastering macOS Programming
- 數(shù)據(jù)結(jié)構(gòu)(C語(yǔ)言)
- Flux Architecture
- C#程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- OpenGL Data Visualization Cookbook
- Delphi開(kāi)發(fā)典型模塊大全(修訂版)
- 現(xiàn)代C:概念剖析和編程實(shí)踐
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- Python硬件編程實(shí)戰(zhàn)
- C語(yǔ)言程序設(shè)計(jì)與應(yīng)用實(shí)驗(yàn)指導(dǎo)書(第2版)