- 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.
推薦閱讀
- Extending Jenkins
- Advanced Quantitative Finance with C++
- 計算思維與算法入門
- Flink SQL與DataStream入門、進階與實戰(zhàn)
- HTML5權威指南
- R語言數(shù)據(jù)可視化:科技圖表繪制
- C語言程序設計習題與實驗指導
- SQL Server 2016 從入門到實戰(zhàn)(視頻教學版)
- ASP.NET 4.0 Web程序設計
- IPython Interactive Computing and Visualization Cookbook
- Julia High Performance(Second Edition)
- Functional Python Programming
- WordPress Search Engine Optimization(Second Edition)
- Mastering Unreal Engine 4.X
- Java設計模式深入研究