当前位置:网站首页>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>
边栏推荐
猜你喜欢

Getting started with bladed tutorial (video)

2022.6.7 特长生模拟

【案例解读】医疗单据OCR识别助力健康险智能理赔

C# 微信上传Form-data

2022.6.7 special student simulation

2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings

C language - growth diary-04- preliminary exploration of local variables (local variables)

学习《缠解论语》

(transformation) tree, binary tree and forest transformation principle
![[software testing] 90% of the interviewers have been brushed out of such resumes](/img/2f/bb4819b98592f750dec92d4b4dd6b7.png)
[software testing] 90% of the interviewers have been brushed out of such resumes
随机推荐
Selenium click the floating menu and realize the functions of right mouse button
Use of wordcloud
Note: JDBC
C language three chess games
[atcoder1983] BBQ hard (combination number + clever model transformation)
[noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
JSP development model
Session and session management technology
TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者
Label the mask image not obtained through labelme
【IoT】项目管理:如何打造更好的跨职能团队?
Zero foundation self-study SQL course | outer join external connection
YUV数据的裁剪与重叠
(transformation) tree, binary tree and forest transformation principle
2022.6.7 特长生模拟
[untitled] Weng_ C lesson 1
Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np
Sort - select sort
You got 8K in the 3-year function test, but you were actually pretending to work hard
Data visualization and Matplotlib