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

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

相关推荐
FungLeo35 分钟前
成为全栈·Vue 管理后台篇·Biome、类型检查、构建和API 代理如何组成质量门禁
前端·javascript·vue.js·typescript·vite·前端工程化·biome
泯泷18 小时前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
4cos9020 小时前
pi学习笔记-AgentLoop
typescript
afumu2 天前
让本机 Agent 指挥浏览器里的多个网页 AI:一套 127.0.0.1 控制协议的设计取舍
chrome·typescript
江湖有缘2 天前
基于Ubuntu部署SoduWork:企业级 AI 原生协同平台实战全流程
linux·人工智能·ubuntu·ai·soduwork
濮水大叔3 天前
Cabloy/Vona 为什么不需要 NestJS 的 Dynamic Module
typescript·node.js·nestjs
浪遏3 天前
recruitment-cli:让 AI 接手求职招聘,从架构到一次发送超时
架构·typescript·node.js
律宏阔4 天前
微信小程序 + Taro 从 ESLint 迁移到 Oxlint 让项目代码检查速度提升近 12 倍
前端·typescript·eslint
瓦瓦gogo4 天前
从一个 TypeScript 函数开始理解 MCP 协议
linux·ubuntu·typescript
鱼听禅4 天前
Ubuntu安装YOLOv8进行菌落识别训练
linux·python·yolo·ubuntu