当前位置:网站首页>TypeScript-unknown类型
TypeScript-unknown类型
2022-06-11 07:54:00 【YY小怪兽】
什么是unknown类型?
unknown类型是TS3.0中新增的一个顶级类型, 被称作安全的any
1.任何类型都可以赋值给unknown类型
let value:unknown;
value = 123;
value = "abc";
value = false;
2.如果没有类型断言或基于控制流的类型细化, 那么不能将unknown类型赋值给其它类型
let value1:unknown = 123;
let value2:number;
value2 = value1;//报错
value2 = value1 as number;//类型断言
if(typeof value1 === 'number'){
//基于控制流的类型细化
value2 = value1;
}
3.如果没有类型断言或基于控制流的类型细化, 那么不能在unknown类型上进行任何操作
let value1:unknown = 123;
value1++;//报错
(value1 as number)++;
if(typeof value1 === 'number'){
value1++;
}
4.只能对unknown类型进行 相等或不等操作, 不能进行其它操作(因为其他操作没有意义)
let value1:unknown = 123;
let value2:unknown = 123;
console.log(value1 === value2);
console.log(value1 !== value2);
console.log(value1 >= value2); // 虽然没有报错, 但是不推荐, 如果想报错提示, 可以打开严格模式
5.unknown与其它任何类型组成的交叉类型最后都是其它类型
//type MyType = number
type MyType = number & unknown;
//type MyType = string;
type MyType = unknown & string;
6.unknown除了与any以外, 与其它任何类型组成的联合类型最后都是unknown类型
//type MyType = any;
type MyType = unknown | any;
//type MyType = unknown
type MyType = unknown | number;
//type MyType = unknown
type MyType = unknown | string | boolean;
7.never类型是unknown类型的子类型
type MyType = true
type MyType = never extends unknown ? true : false;
8.keyof unknown等于never
//type MyType =never
type MyType = keyof unknown;
9.unknown类型的值不能访问其属性,方法,创建实例
class Person {
name:string = 'lnj';
say():void{
console.log(`name = ${
this.name}`);
}
}
let p:unknown = new Person();
p.say();//报错
console.log(p.name);//报错
10.使用映射类型时, 如果遍历的是unknown类型, 那么不会映射任何属性
type MyType<T> = {
[P in keyof T]:any
}
//type res = {}
type res = MyType<unknown>
边栏推荐
- Crawl Baidu Baipin dynamic page
- The solution of "no startup device" after running Bochs
- VIM common commands
- forEach 中 return 和 for 中 break
- TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
- Space geometry
- C language Yanghui triangle code
- wordcloud的使用
- 用 Keras/TensorFlow 2.9 创建深度学习模型的方法总结
- [atcoder2000] leftmost ball (dp+ combination number)
猜你喜欢

Detailed explanation of character function and string function (including simulation implementation)

Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)

如何做好空状态设计?来看这份全面总结

C language - growth diary-04- preliminary exploration of local variables (local variables)
![Socket [5] - struct linker usage](/img/e5/bce5d89c61a3a4f89171734ca6ff8e.png)
Socket [5] - struct linker usage

记一次忽略@SuppressLint(“NewApi“)提示引发的血案
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

空间几何

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

【 史上最全的ENSP【安装图解】!】
随机推荐
You got 8K in the 3-year function test, but you were actually pretending to work hard
Printing diamond of beginner C
How to prepare for the new PMP syllabus exam?
C language lesson 2
Sort - Swap sort
Long dialogue in June 2017
Figure seamless database integration tushare interface
Magnifying mirror rendering
[atcoder2307] tree game
Servlet、ServletConfig、ServletContext
【 史上最全的ENSP【安装图解】!】
134. 加油站
零基础自学SQL课程 | UNION 联合查询
2021-10-24
Tutoriel de démarrage bladed (vidéo)
[atcoder2306] rearranging (topology)
使用 COCO 数据集训练 YOLOv4-CSP 模型
[atcoder2000] leftmost ball (dp+ combination number)
Selenium click the floating menu and realize the functions of right mouse button
Database connection pool and bdutils tool