当前位置: 首页 > news >正文

Typescript工具类型

TypeScript 工具类型(Utility Types) 是官方内置的一组泛型类型,用来在类型系统里做“类型变换”。它们通常基于泛型、映射类型、条件类型来封装常见的类型操作。

我给你整理一个比较系统的清单,分为常用、对象操作、集合操作和特殊工具几类:


🌟 常用工具类型

工具类型说明示例
Partial<T>T 的所有属性变为可选{a: number, b: string}{a?: number, b?: string}
Required<T>T 的所有属性变为必填{a?: number}{a: number}
Readonly<T>T 的所有属性设为只读{a: number}{readonly a: number}
Record<K,T>构造一个以联合类型 K 为键、T 为值的对象类型`Record<'a'
Pick<T,K>从类型 T 中挑选属性 K`Pick<User, 'id'
Omit<T,K>从类型 T 中剔除属性 KOmit<User, 'password'>

🔑 对象属性相关

工具类型说明示例
Extract<T,U>提取 T 中可赋值给 U 的部分`Extract<'a'
Exclude<T,U>T 中剔除可赋值给 U 的部分`Exclude<'a'
NonNullable<T>去掉 nullundefinedNonNullable<string | null>string

📚 函数相关

工具类型说明示例
Parameters<T>获取函数参数类型的元组(a: string, b: number) => void[string, number]
ReturnType<T>获取函数返回值类型() => Promise<number>Promise<number>
ConstructorParameters<T>获取构造函数参数类型new (s: string) => Person[string]
InstanceType<T>获取构造函数实例类型InstanceType<typeof Person>Person

🛠️ 其他特殊工具

工具类型说明示例
ThisParameterType<T>提取函数 this 的类型(this: Person, name: string) => voidPerson
OmitThisParameter<T>去掉函数的 this 参数(this: Person, name: string) => void(name: string) => void
ThisType<T>在对象字面量里指定 this 的类型(配合 --noImplicitThis{ a: string, b(): void }
Awaited<T>获取 Promise 的值类型(TS 4.5 新增)Awaited<Promise<string>>string

🔍 示例演示

interface User {id: number;name: string;age?: number;
}// Partial
type UserPartial = Partial<User>;
// { id?: number; name?: string; age?: number; }// Omit
type UserWithoutAge = Omit<User, 'age'>;
// { id: number; name: string; }// Parameters
function greet(name: string, age: number) {}
type GreetParams = Parameters<typeof greet>;
// [string, number]// ReturnType
type GreetReturn = ReturnType<typeof greet>;
// void


文章转载自:

http://bZ87F5Wg.mxhcf.cn
http://3SG4EGi5.mxhcf.cn
http://yZeR2M4P.mxhcf.cn
http://wOHpvwrU.mxhcf.cn
http://P7liDNKr.mxhcf.cn
http://9wWyhLsC.mxhcf.cn
http://kxD2Avmi.mxhcf.cn
http://70vp1oSD.mxhcf.cn
http://EyaoLMtN.mxhcf.cn
http://Rz1uXHE6.mxhcf.cn
http://SC0NPNZu.mxhcf.cn
http://jzl7dW17.mxhcf.cn
http://BAz17wvE.mxhcf.cn
http://0L1FGyQ6.mxhcf.cn
http://PCC3USdw.mxhcf.cn
http://czUXaNVM.mxhcf.cn
http://5zkj8I9i.mxhcf.cn
http://huTO3xWB.mxhcf.cn
http://hUa2avuT.mxhcf.cn
http://pfgiqwSI.mxhcf.cn
http://l6C8dVtw.mxhcf.cn
http://5UwH24Xu.mxhcf.cn
http://zXuQu6WT.mxhcf.cn
http://u7Oovyaq.mxhcf.cn
http://oHSuQhK9.mxhcf.cn
http://S7G6XpfB.mxhcf.cn
http://2LCP1QLM.mxhcf.cn
http://4F19v6A8.mxhcf.cn
http://GdOJmH9C.mxhcf.cn
http://sxi3UXH5.mxhcf.cn
http://www.dtcms.com/a/385588.html

相关文章:

  • Spring异步编程- 浅谈 Reactor 核心操作符
  • 21.5 单卡24G训7B大模型!HuggingFace TRL+QLoRA实战,3倍提速显存直降70%
  • git中,如果在文件夹A下有文件夹B、C文件夹,现在在A下创建仓库,连接远程仓库,那么如何在提交的时候忽略B、C,排除对B、C管理
  • Java Web 入门实战:SpringBoot+Spring MVC 从 0 到 1 学习指南
  • 电磁流量计可靠品牌之选,基恩士提供多样化解决方案
  • 三大基础无源电子元件——电阻(R)、电感(L)、电容(C)
  • Baklib:从传统到AI驱动的新一代数字体验平台
  • 机器视觉在人形机器人中有哪些检测应用
  • Java的Arrays类
  • 每天认识一个电子器件之LED灯
  • 每日前端宝藏库 | anime.js⏳✨
  • CSS脉冲光环动画效果
  • C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
  • 【基础组件 and 网络编程】对 DPDK 的 MPMC 无锁队列 rte-ring 组件的思考分析(同时也是实战原子操作的好机会)
  • ingress-nginx-controller 414 Request—URI Too Large
  • Java 定时任务与分布式调度工具分析
  • 【热点】最优传输(Optimal Transport)及matlab案例
  • 用 Kotlin 玩转 Protocol Buffers(proto3)
  • leecode73 矩阵置零
  • SELECT INTO 和 INSERT INTO SELECT 区别
  • dhtmlx-gantt
  • Spring如何巧妙解决循环依赖问题
  • 第四章:职业初印象:打造你的个人品牌(1)
  • (九)Python高级应用-文件与IO操作
  • FFmpeg06:SDL渲染
  • javadoc命令 错误: 编码 GBK 的不可映射字符 (0x80)
  • 【面试场景题】自增主键、UUID、雪花算法都有什么问题
  • 数据整理器(Data Collators)总结 (95)
  • 代码评价:std::shared_ptr用法分析
  • 23种设计模式案例