当前位置:网站首页>【ts】typescript高阶:联合类型与交叉类型
【ts】typescript高阶:联合类型与交叉类型
2022-08-05 05:16:00 【六月的可乐】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
typescript高阶之联合类型与交叉类型
前言
学习目标
1、联合类型与类型收缩的结合使用
2、交叉运算符特性、与交叉运算
3、工具类型.PartialByKeys的实现
4、泛型变量TKVE与显式、隐式指定
一、联合类型与类型收缩的结合使用
// 联合类型 和类型收缩
function greet(person: string | string[]): string | string[] {
if (typeof person === 'string') {
return `你好呀我是${
person}返回了`
} else if (Array.isArray(person)) {
return `哈哈哈${
person}`
}
throw new Error('this is error');
}
二、交叉运算符特性、与交叉运算
1.交叉运算符特性
- 唯一性: A&A等于A
- 满足交换律:A&B等价于B&A
- 满足结合律:(A&B)&C 等价于A&(B&C)
- 父类型收敛:如果B是A的父类型,则A&B将被收敛到A类型
代码如下(示例):
type PonitA = {
x: string;
y: number;
};
type PointB = {
name: string;
};
type PointC = PointB & PonitA; //{x: string;y:number;name:string}
2.交叉运算
// 复杂类型的交叉运算
type D = {
d: boolean;
}
type E = {
e: string;
}
type F = {
f: number;
}
type Test1 = {
x: D
}
type Test2 = {
x: E
}
type Test3 = {
x: F
}
type Test4 = Test1 & Test2 & Test3;// {x:{d: boolean;e: string;f:number}}
// 简单类型的交叉运算
// 简单类型的交叉运算
type Foo = {
name: string;
age: number,
color: boolean;// 注意:这里boolean可以视作由true|false两个字面量的联合类型
};
type Boo = {
name: boolean;
age: number,
color: string
};
type Fb = Foo & Boo;// never
// 函数类型的交叉运算
type F1 = (x: string, y: string) => void;
type F2 = (x: number, y: number) => void;
type F3 = F1 & F2;// (x: string | number, y: number | string) => void;
let fn: F3 = (x: string | number, y: number | string) => {
};
fn('helo', 'word');//正确
//fn('helo', 1);//错误
fn(1, 2);//正确
//fn(3, 'word');//错误
// 如果希望以上错误修复可以这样
type F4 = (x: string, y: number) => void;
type F5 = (x: number, y: string) => void;
let fn2: F1 & F2 & F4 & F5 = (x: string | number, y: string | number) => {
};
fn2('hello', 1);//正确
fn2(1, '222');//正确
3.PartialByKeys
功能:将一对象类型的部分key类型变成可选
思路:
1、将传入的key类型变成可选
2、pick出除了传入的这几个key之外的没有变成可选的key
3、将变成可选的和没有变成可选的交叉运算一起
// PartialByKeys
//工具类型
type PartialByKeys<T, U extends keyof T> = {
[K in U]?: T[K]
} & Pick<T, Exclude<keyof T, U>>;
type U1 = {
name: string;
age: number;
choose: boolean;
};
type U2 = PartialByKeys<U1, 'name'>;// {name?: string;age:number;choose: boolean};
let user: U2 = {
age: 11,
choose: true
};//正确
4、泛型变量TKVE与显式、隐式指定
范型变量:
T(type):表示类型
K(key):表示对象中键的类型
V(value):表示对象中值的类型
E(Element):表示元素类型
这些都是约定俗成的写法
// 显式指定泛型
function foo1<T, R>(x: T, y: R) {
}
foo1<string, number>('111', 2); // function foo1<string, number>(x: string, y: number): void
// 隐式指定泛型
foo1(22, '111')// function foo1<number, string>(x: number, y: string): void
总结。
边栏推荐
- 单变量线性回归
- My 的第一篇博客!!!
- 如何编写一个优雅的Shell脚本(二)
- Spark ML学习相关资料整理
- spingboot 容器项目完成CICD部署
- 【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
- 【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
- [Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
- 「实用」运维新手一定不能错过的17 个技巧
- 如何跟踪网络路由链路&检测网络健康状况
猜你喜欢

OSPF网络类型

大型Web网站高并发架构方案

【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)

A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.

解决:Unknown column ‘id‘ in ‘where clause‘ 问题

【数据库和SQL学习笔记】10.(T-SQL语言)函数、存储过程、触发器
![[Go through 9] Convolution](/img/84/e6d99793aacf10a7b099f60bcaf290.png)
[Go through 9] Convolution

物联网:LoRa无线通信技术

哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
![[Go through 4] 09-10_Classic network analysis](/img/f2/e6e71869b8ab014cc1eea0537fc2e7.png)
[Go through 4] 09-10_Classic network analysis
随机推荐
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
基于STM32F4的FFT+测频率幅值相位差,波形显示,示波器,时域频域分析相关工程
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
表情捕捉的指标/图像的无参考质量评价
A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
OSPF网络类型
vscode要安装的插件
数控直流电源
CVPR 2022 |节省70%的显存,训练速度提高2倍
读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping
Kubernetes常备技能
单变量线性回归
You should write like this
服务网格istio 1.12.x安装
基于Flink CDC实现实时数据采集(二)-Source接口实现
Web Component-处理数据
《基于机器视觉测量系统的工业在线检测研究》论文笔记
如何编写一个优雅的Shell脚本(二)
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码