当前位置:网站首页>《21天精通TypeScript-4》-类型推断与语义检查
《21天精通TypeScript-4》-类型推断与语义检查
2022-07-25 18:36:00 【搞前端的半夏】
大家好,我是半夏,一个热爱分享的前端程序员(半吊子后端程序员).如果喜欢我的文章,可以关注 点赞 加我微信:frontendpicker,一起学习交流前端,成为更优秀的工程师~关注公众号:搞前端的半夏,了解更多前端知识! 点我探索新世界!
专栏地址
https://blog.csdn.net/shangyanaf/category_11822476.html

类型推断
在编码区域中,输入以下代码:
let userName= '123'
当你将鼠标放置在该串代码处,会看到TS推断出userName的类型是string

当你添加以下代码
userName = 18
此时该串代码下方会出现红色的波浪线。TS会提示你:Type ‘number’ is not assignable to type ‘string’.

在JS中,我们可以给一个变量分配不同类型的值,例如这里的userName你可以给他赋值字符串,也可以赋值为数字。
这种不重视变量类型的写法,可能会带来一些不易察觉的错误。
在TS中,当我们给一个变量初始化是字符串类型,后面我们又给他分配了数字类型!TS会抛出具体的错误信息。
这也就是TS的类型推断。当我们初始化一个变量,并赋值。TS会从赋值中推断出该类型。例如:let userName= '123',TS
会推断出userName的类型是字符串。
当然除了简单的类型之外,TS类型推断还支持Object和Array类型推断。
例如下面的user对象,TS会自动推断user对象的类型为:{name:"搞前端的半夏"},并且内部属性name的类型也可以推断出来。
let user={
name:"搞前端的半夏"
}


同样的类型推断也适用于函数返回。
例如下面这个示例,result接受了函数的返回值0,当鼠标放在result是,TS会提示result是number类型。
function test() {
return 0;
}
let tesult=test()

语义分析
百度百科:语义分析是编译过程的一个逻辑阶段, 语义分析的任务是对结构上正确的源程序进行上下文有关性质的审查,进行类型审查。
在TS中,语义分析阶段分为:
- 作用域分析
- 类型检查
TS的一个目标就是通过静态分析代码,在开发过程中提前知道代码中的可能错误。
例如下面的代码,这是一个极端的例子,应该不会有人这么写吧。在test函数内部,有一个for循环,很不幸的是,我们不小心在循环的时候,使用了const定义i。
function test(){
for(const i=0;i<100;i++){
}
}
在TS环境下:

在JS环境下:

边栏推荐
- Tang's little helper
- 用GaussDB(for Redis)存画像,推荐业务轻松降本60%
- [web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
- [QNX Hypervisor 2.2用户手册]9.5 dump
- [haoi2015] tree operation
- 动态内存管理
- SQL那些事
- Tensor to img && imge to tensor (pytorch的tensor转换)
- How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
- Safe operation instructions for oscilloscope probe that must be read by engineers
猜你喜欢

Optimistic lock resolution

Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree

What is the difference between GB and gib disk space units?

Experiment 2 goods purchase and sale management system

软件测试——常用的测试工具

这届年轻人,为“丑东西”有多上头?

TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format

SQL那些事

Tkinter GUI address book management system

rust多线程安全计数
随机推荐
What is 3DE experience platform
[QNX Hypervisor 2.2用户手册]9.5 dump
SQL那些事
中信证券低佣金开户免5是真的吗,安全吗
What are the methods of traversing arrays? What is the difference between the performance of the for loop foreach for/in for/of map and how to choose?
电流探头在ECU、电气系统电流测量的应用
【小程序开发】页面导航详解
JZ32 从上往下打印二叉树
[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
Ceres curve fitting
Insufficient space on Disk C mklink solves vscode expansion migration to other disks
Optimistic lock resolution
String function and memory function (2)
JVM基础和问题分析入门笔记
Tkinter GUI address book management system
3de 回复
Dynamic memory management
ORB_ Slam3 recurrence - Part I
Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
[QNX Hypervisor 2.2用户手册]9.4 dryrun