type 类型别名
type Name = string
type Age = number
type Man = {
name: Name
age: Age
}
const c: Man = {
name: 'c',
age: 20
}
keyof 对象键类型
type _Object = { [key: string]: unknown }
function getKeysByObj<T extends _Object>(o: T): (keyof T)[]{
return Object.keys(o)
}
const a = {
name: 'a',
age: 12
}
const aKeys = getKeysByObj(a)
const b = {
1: 'b',
2: 20
}
const bKeys = getKeysByObj(b)
typeof 值类型
const a = {
name: 'a',
age: 23
}
const b: typeof a = {
name: 'b',
age: 10
}
function random(n: number): number{
return Math.random() * n
}
type Random = typeof random
Partial 转为可选参数
interface Man {
name: string,
age: number
}
type ManCopy = Partial<Man>
const cache: ManCopy = {}
Required 将属性全转为必填
interface Data {
id: string
type?: string
}
type FullData = Required<Data>
const fd: FullData = {
id: 'xx',
type: 'su'
}
Readonly 属性都转为只读
interface Data {
id: string
type?: string
}
type ReadonlyData = Readonly<Data>
const d:ReadonlyData = {
id: 'xx',
}
d.id = 'cc'
Record<K, T> 定义 K 为键, T 为值的映射类型
type keys = 'name' | 'job' | 'age'
type Man = Record<keys, string>
const m: Man = {
name: 'm',
job: 't',
age: '10',
}
Pick 提取指定属性,生成新类型
interface Man {
id: string
name: string
age: number
job: string
birth: string
address: string
}
type ManIt = Pick<Man, 'name' | 'age' | 'job'>
const m: ManIt = {
name: 'm',
age: 25,
job: 'tt'
}
Omit 删除指定属性,生成新类型
type A = 'color' | 'width' | 'height'
type B = {
name: string
width: number
height: number
}
type C = Omit<B, A>
Exclude<K, T> 联合类型取 K,T 的差集
interface A{
name: string
color: string
}
interface B{
name: string,
width: number
}
type keys = Exclude<keyof A, keyof B>
type C = Record<keys, string>
const c: C = {
color: 'orange'
}
Extract<A, B> 联合类型取 A, B的并集
type A = 'color' | 'width' | 'height'
type B = 'name' | 'height'
type C = Extract<A, B>
NonNullable 过滤类型内的 null 、 undefined、never
type A = 'data' | null | undefined | never
type B = NonNullable<A>
type T1 = NonNullable<null>
type T2 = NonNullable<undefined>
type T3 = NonNullable<never>
type T4 = NonNullable<any>
type T5 = NonNullable<string>
Parameters 获取函数参数类型元组
function buildCar(name: string, color: string){
return { name, color }
}
const args: Parameters<typeof buildCar> = ['xx', 'yy']
console.log(buildCar(...args))
ConstructorParameters 获取构造函数参数类型元组
class Car{
name:string
color:string
constructor(name: string, color:string){
this.name = name
this.color = color
}
}
interface CarConstructor{
new (name: string, color:string): Car
}
const args:ConstructorParameters<CarConstructor> = ['mini', 'blue']
const c = new Car(...args)
InstanceType 获取构造函数返回值类型
class Car{
name:string
color:string
constructor(name: string, color:string){
this.name = name
this.color = color
}
}
interface CarConstructor{
new (name: string, color:string): Car
}
type c = InstanceType<CarConstructor>
ReturnType 函数返回值类型
function getNumber(): number{
return Math.random()
}
function getString(): string{
return `${Math.random()}`
}
let a:ReturnType<typeof getNumber>
let b: ReturnType<typeof getString>
a = 10
b = 'msg'
Uppercase 字符字面量全大写
type Status = 'success' | 'fail'
type STATUS = Uppercase<Status>
Lowercase 字符字面量全小写
type Status = "SUCCESS" | "FAIL"
type status = Lowercase<Status>
Capitalize 字符字面量首字母大写
type Status = "success" | "FAIL"
type status = Capitalize<Status>
UnCapitalize 字符字面量首字母小写
type Status = "success" | "FAIL"
type status = Uncapitalize<Status>
ThisType 明确当前函数this的指向类型
const foo = {
bar() {
console.log(this.a);
}
}
const foo: { bar: any } & ThisType<{ a: number }> = {
bar() {
console.log(this.bar)
console.log(this.a);
}
}
foo.bar
foo.a
ThisParameterType 获取函数this指向类型
interface Car{
name: string
color: string
}
function getCarName(this: Car){
console.log(this.name)
}
const c = {
name: 'x',
color: 'y',
}
getCarName.apply(c)
type T = ThisParameterType<typeof getCarName>
OmitThisParameter 获取一个没有this指向的函数类型
interface Car{
name: string
color: string
}
function getCarName(this: Car){
console.log(this.name)
}
type T = OmitThisParameter<typeof getCarName>
const fn: T = () => {
console.log('not name')
}
infer 替代推断类型
type Unpacked<T> = T extends (infer U)[] ? U : T;
type T0 = Unpacked<string[]>;
type T1 = Unpacked<string>;
文档