当前位置:网站首页>TypeScript-可辨识联合
TypeScript-可辨识联合
2022-06-11 07:54:00 【YY小怪兽】
详情可见
1.什么是可辨识联合
具有共同的可辨识特征。
一个类型别名, 包含了具有共同的可辨识特征的类型的联合。
interface Square {
kind: "square"; // 共同的可辨识特征
size: number;
}
interface Rectangle {
kind: "rectangle"; // 共同的可辨识特征
width: number;
height: number;
}
interface Circle {
kind: "circle"; // 共同的可辨识特征
radius: number;
}
Shape就是一个可辨识联合
因为: 它的取值是一个联合
因为: 这个联合的每一个取值都有一个共同的可辨识特征
type Shape = (Square | Rectangle | Circle);
function aera(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius ** 2; // **是ES7中推出的幂运算符
}
}
2.在企业开发中如果相对可辨识联合的完整性进行检查, 那么我们可以使用
为了需要写全部类型的判断,我们需要进行完整性检查,
当不完整的时候报错
function aera(s: Shape):number{
switch (s.kind) {
case "square": return s.size * s.size;
// case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius ** 2; // **是ES7中推出的幂运算符
default:return MyNever(s)
}
}
方式一: 给函数添加返回值 + tsconfig.json中开启strictNullChecks
方式二: 添加default + never 不常用
function MyNever(x: never):never {
throw new Error('可辨识联合处理不完整' + x);
}
function aera(s: Shape):number{
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius ** 2; // **是ES7中推出的幂运算符
default:return MyNever(s)
}
}
边栏推荐
- [codeforces1019e] raining season
- Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...
- Bubble sorting with C language
- 如何开始参与开源社区
- Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho
- 远程办公经验分享 | 社区征文
- 2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
- C# 微信上传Form-data
- [atcoder2306] rearranging (topology)
- Semiconductor memory classification
猜你喜欢

C language Yanghui triangle code
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?

Zero foundation self-study SQL course | outer join external connection

Zero foundation self-study SQL course | union joint query

Selenium click the floating menu and realize the functions of right mouse button

Servlet

Collation of basic knowledge of intermediate development of Andrews (for interview)

Figure seamless database integration tushare interface

C language function stack frame

Storage of floating point in memory
随机推荐
Implementation of stack (C language)
Rabin Miller prime test
关于网络知识的50个问答题,你能答对几个?
Clipping and overlapping of YUV data
Detailed explanation of shift operator and bit operator in C language
Request request object and response response object
Modular linear equations (Chinese remainder theorem + general solution)
C language to achieve a simple game - minesweeping
彻底记住ImageView的background和src的区别
Sort - merge sort
Euler's theorem and its extension (with proof)
[atcoder1981] short diameter (graph theory thinking)
2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
C language to achieve minesweeping games
【AtCoder2304】Cleaning
C language - Growth Diary -02- function
Black Qunhui dsm7.0.1 physical machine installation tutorial
Post - form data of interface test
[IOT] project management: how to build a better cross functional team?
Lesson 1 about Xiaobai's C language