当前位置:网站首页>21 days proficient in typescript-4 - type inference and semantic check
21 days proficient in typescript-4 - type inference and semantic check
2022-07-25 18:54:00 【Do front-end Pinellia ternata】
Hello everyone , I am Pinellia ternata , A front-end programmer who loves sharing ( Half a dozen back-end programmers ). If you like my article , Can pay attention to give the thumbs-up Add me WeChat :frontendpicker, Learn to communicate together , Become a better Engineer ~ Official account : Pinellia ternata , Learn more about the front end ! Am I Explore the new world !
Column address
https://blog.csdn.net/shangyanaf/category_11822476.html

Type inference
In the coding area , Enter the following code :
let userName= '123'
When you place the mouse over the string of code , Will see TS Deduce userName The type is string

When you add the following code
userName = 18
At this time, a red wavy line will appear under the string of codes .TS You will be prompted :Type ‘number’ is not assignable to type ‘string’.

stay JS in , We can assign different types of values to a variable , Like here userName You can assign him a string , It can also be assigned to a number .
This kind of writing that does not pay attention to variable types , It may bring some imperceptible mistakes .
stay TS in , When we initialize a variable with a string type , Later, we assigned him a number type !TS Specific error messages will be thrown .
This is the same. TS Type inference of . When we initialize a variable , And the assignment .TS This type will be inferred from the assignment . for example :let userName= '123',TS
Will infer that userName The type of is string .
Of course, in addition to simple types ,TS Type inference also supports Object and Array Type inference .
For example, the following user object ,TS Automatically infer user The type of object is :{name:" Pinellia ternata "}, And internal properties name The type of can also be inferred .
let user={
name:" Pinellia ternata "
}


The same type inference applies to function returns .
For example, the following example ,result Accepted the return value of the function 0, When the mouse is on result yes ,TS Will prompt result yes number type .
function test() {
return 0;
}
let tesult=test()

Semantic analysis
Baidu Encyclopedia : Semantic analysis is a logical stage in the compilation process , The task of semantic analysis is to review the context related properties of the structurally correct source program , Conduct type review .
stay TS in , The semantic analysis stage is divided into :
- Scope analysis
- Type checking
TS One of our goals is to statically analyze code , Know the possible errors in the code in advance during the development process .
For example, the following code , This is an extreme example , No one should write that . stay test Internal function , There is one for loop , unfortunately , We are not careful when cycling , Used const Definition i.
function test(){
for(const i=0;i<100;i++){
}
}
stay TS In the environment :

stay JS In the environment :

边栏推荐
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- 对迁移学习中域适应的理解和3种技术的介绍
- 《21天精通TypeScript-4》-类型推断与语义检查
- JMeter performance test actual video (what are the common performance test tools)
- Software testing process (mind map)
- The understanding of domain adaptation in transfer learning and the introduction of three technologies
- Communication between processes (pipeline communication)
- Go代码检查工具
- 浏览器内核有几种,浏览器版本过低怎么升级
- 软件测试(思维导图)
猜你喜欢

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

ThreadLocal Kills 11 consecutive questions

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

15. Simple salary management system design
![[open source project] stm32c8t6 + ADC signal acquisition + OLED waveform display](/img/5f/413f1324a8346d7bc4a9490702eef4.png)
[open source project] stm32c8t6 + ADC signal acquisition + OLED waveform display

What is hpapaas platform?

Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!

Analysis of the internet jam in IM development? Network disconnection?

SQL realizes 10 common functions of Excel, with original interview questions attached
随机推荐
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
Typescript object proxy use
接口自动化测试平台FasterRunner系列(二)- 功能模块
7/24 training log
Interface automation test platform fasterrunner series (IV) - continuous integration and solution of multi domain names
App test point (mind map)
Typescript反射对象Reflect使用
怎么禁止使用360浏览器(怎么才能把自带的浏览器停用)
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
hough变换理解[通俗易懂]
Address book (II)
ThreadLocal夺命11连问
Excellent test / development programmers should make breakthroughs and never forget their original intentions, so that they can always
2022 Robocom 省赛题解
浏览器内核有几种,浏览器版本过低怎么升级
「Wdsr-3」蓬莱药局 题解
jmeter性能测试实战视频(常用性能测试工具有哪些)
【加密周报】加密市场有所回温?寒冬仍未解冻!盘点上周加密市场发生的重大事件!
什么是3DE体验平台
The Yellow Crane Tower has a super shocking perspective. You've never seen such a VR panorama!