typescript进阶语法

typescript进阶语法

interface 接口定义

c 复制代码
interface userType {
	name:string,
	age:number,
	sex?:string
}

type接口定义

c 复制代码
type userType= {
	name:string,
	age:number,
	sex?:string
}
type userType ='username'  # 固定值写法
let user:userType = 'age'  # 报错 只能等于username

pick摘取部分类型

c 复制代码
type userType= {
	name:string,
	age:number,
	sex?:string
}
type myType=Pick<userType,'name'|'age'>
let my:myTyp={
	name:'张三',
	age:18
}

omit 剔除属性

c 复制代码
type userType= {
	name:string,
	age:number,
	sex:string
}
type myType=Pink<userType,'name'|'age'>
let my:myTyp={
	sex:'男'  # 加上其他属性就会报错,因为属性被剔除
}

Exclude 排除

c 复制代码
type a = 'a'|'b'|'c'
type b = 'b'|'c'
type Atype=Exclude<a,b> # a是否继承b 是 nerve 否 a,简单说a的类型b中是否有,有的话就是nerve空,否则就是a类型 相当于剔除
let data:Atype='a'

Extract 选择

c 复制代码
type a = 'a'|'b'|'c'
type b = 'b'|'c'
type Atype=Exclude<a,b> # 取a跟b的交集
let data:Atype='b'
let data2:Atype='c'

总结:当我们知道一组类型中需要剔除哪些属性时,就用Pink跟omit,当我们已知两组类型,但又不知道剔除哪些类型的时候就用Extract ,Exclude

Partial

将所有的属性变为可选属性

相关推荐
敲代码还房贷8 小时前
解决win7装不了vmware tools问题
ubuntu
naildingding10 小时前
3-ts接口 Interface
前端·typescript
下午写HelloWorld11 小时前
Linux系统及Ubuntu常用指令
linux·ubuntu·操作系统
ZengLiangYi11 小时前
TypeScript 项目配置:tsconfig、ESM、路径别名
javascript·typescript·aigc
哆啦A梦——16 小时前
Ubuntu 虚拟机 Docker 与 MySQL 8.0.42 部署指南
mysql·ubuntu·docker
上天_去_做颗惺星 EVE_BLUE17 小时前
【新 Linux 服务器上手全攻略】系统巡检、存储规划与开发环境初始化
linux·运维·服务器·ubuntu·macos·centos
退休倒计时17 小时前
【每日一题】LeetCode 15. 三数之和 TypeScript
数据结构·算法·leetcode·typescript
das2m17 小时前
WSL2 Ubuntu 配置完美版 docker compose 指南
linux·ubuntu·docker
MIXLLRED17 小时前
Ubuntu 22.04 + ROS2 Humble 上部署 ScaRF‑SLAM指南
ubuntu·slam·ros2·离线建图
FlyWIHTSKY18 小时前
JavaScript 和 TypeScript 分别是什么,可以相互写吗
javascript·ubuntu·typescript