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

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

相关推荐
猿与禅4 分钟前
Linux(Ubuntu)入门到实战:系统管理、常用命令与权限体系完整指南
linux·ubuntu·apt·进程管理·系统运维·用户权限·vi/vim
西瓜太郎12345 分钟前
外层模型卡片与分组详情成功率不一致,应该怎么排查?
前端·typescript·go·软件工程·react
掰头战士6 小时前
MCP、Skill、Plugin,都是给agent拓展能力,到底有何区别?
typescript·llm·agent
cui_hao_nan8 小时前
从零搭建 Kubernetes 集群:Ubuntu + kubeadm 完整实战
ubuntu·容器·kubernetes
掰头战士9 小时前
聊聊 Function Calling,你的模型是否答非所问?
前端·typescript·agent
dsyyyyy110111 小时前
Typescript装饰器
前端·javascript·typescript
金色熊族12 小时前
windows11 + wsl2 上安装ubuntu20.04
ubuntu·windows11·wsl2
WX _ jishuwu199013 小时前
esmfold gpu模式 批量预测蛋白三级结构实况记录 - 供参考
人工智能·ubuntu·三代测序·亚细胞定位,·deeploc·protcompv9
微软技术分享15 小时前
基于 HuggingFace Tokenizers 训练自定义分词器
python·ubuntu·大模型·huggingface
皓月盈江16 小时前
Linux系统使用VSCode搭建GO开发环境
linux·vscode·ubuntu·golang·debian