当前位置:网站首页>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 :

边栏推荐
- Typescript reflection object reflection use
- PHP 中的跨站请求伪造
- [applet development] page navigation details
- 什么是3DE体验平台
- Process communication (Systemv communication mode: shared memory, message queue, semaphore)
- How to create an effective help document?
- Circulaindicator component, which makes the indicator style more diversified
- ES6 implements the observer mode through proxy and reflection
- Yyds dry inventory interview must brush top101: reverse linked list
- Interface automation test platform fasterrunner series (IV) - continuous integration and solution of multi domain names
猜你喜欢

微软Azure和易观分析联合发布《企业级云原生平台驱动数字化转型》报告

How to design product help center? The following points cannot be ignored

对迁移学习中域适应的理解和3种技术的介绍

SQL realizes 10 common functions of Excel, with original interview questions attached

HTTP缓存通天篇,可能有你想要的

How high are the young people in this class for "ugly things"?

MySQL子查询篇(精选20道子查询练习题)

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?

3de 回复

Ultimate doll 2.0 | cloud native delivery package
随机推荐
「Wdsr-3」蓬莱药局 题解
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
F5:企业数字化转型所需六大能力
蓝牙协议详解(蓝牙是什么)
PHP等于==和恒等于===的区别
App test point (mind map)
年轻时代,噢,年轻时代
Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
什么是hpaPaaS平台?
优维低代码:Use Resolves
[translation] logstash, fluent, fluent bit, or vector? How to choose the right open source log collector
Baklib:制作优秀的产品说明手册
JMeter performance test actual video (what are the common performance test tools)
信达证券是国企吗?在信达证券开户资金安全吗?
The Yellow Crane Tower has a super shocking perspective. You've never seen such a VR panorama!
【开源工程】STM32C8T6+ADC信号采集+OLED波形显示
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
上半年出货量已超去年全年,森思泰克毫米波雷达“夺食”国际巨头
接口自动化测试平台FasterRunner系列(四)- 持续集成、解决多域名
What is 3DE experience platform