- 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.
推薦閱讀
- LabVIEW Graphical Programming Cookbook
- R語言編程指南
- Java面向對象程序開發及實戰
- JSP開發案例教程
- Apex Design Patterns
- HTML5+CSS3網站設計基礎教程
- 從Java到Web程序設計教程
- Unity 3D/2D移動開發實戰教程
- Odoo 10 Implementation Cookbook
- JavaScript程序設計(第2版)
- JSP程序設計實例教程(第2版)
- 多模態數據分析:AGI時代的數據分析方法與實踐
- Mastering Concurrency in Python
- PHP+MySQL Web應用開發教程
- SQL Server實例教程(2008版)