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

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

相关推荐
@@@@@@@@5427 分钟前
JavaScript 的缺点与 TypeScript 的弥补
开发语言·javascript·typescript
用户0934077735142 小时前
HarmonyOS WPS Open SDK 实践:最小打开闭环与 enableEdit 分支处理
typescript·harmonyos
鱼听禅5 小时前
ubuntu学习笔记-使用ls-ml-toolkit工具识别Label Studio数据数据训练yolo模型
笔记·学习·ubuntu
IT曙光15 小时前
ubuntu apt-get离线源制作
linux·ubuntu
柯克七七20 小时前
我把 bug 修得太干净了,测试组以为这个模块本来就没问题
前端·javascript·typescript
退休倒计时21 小时前
【每日一题】LeetCode 39. 组合总和 TypeScript
算法·leetcode·typescript
yyt36304584121 小时前
KlineChartQuant Tooltip 高频交互 200FPS 性能优化完整复盘
前端·经验分享·性能优化·typescript·vue·交互·chrome devtools
源码学社1 天前
Spug 3.0 Ubuntu 22.04 Docker 部署文档
linux·ubuntu·docker·windows安装·spug
tebukaopu1481 天前
ubuntu挂载nfs
linux·运维·ubuntu
用户0934077735141 天前
HarmonyOS WPS Open SDK 实践:办公审批附件「打开—编辑—回传」代码实现
typescript·harmonyos