- 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 2018 虛擬儀器程序設(shè)計
- Learning Apex Programming
- 深入理解Django:框架內(nèi)幕與實現(xiàn)原理
- 跟小海龜學(xué)Python
- Scratch真好玩:教小孩學(xué)編程
- Cassandra Data Modeling and Analysis
- WordPress Plugin Development Cookbook(Second Edition)
- Python機器學(xué)習(xí)實戰(zhàn)
- JavaScript入門經(jīng)典
- iOS開發(fā)項目化入門教程
- Angular Design Patterns
- Python硬件編程實戰(zhàn)
- Python應(yīng)用與實戰(zhàn)
- Get Your Hands Dirty on Clean Architecture
- Offer來了:Java面試核心知識點精講(框架篇)