实用工具

llz大约 3 分钟

Partial

Partial<Type> 将类型的所有属性变为可选的。

interface IUserInfo {
  name: string
  age: number
}

//OK
const userInfo: Partial<IUserInfo> = {
  name: "rice",
}

Reqired

Required<Type> 使类型 Type 的所有属性变成必选的,与 Partial<Type> 相反。

const users:Required<IRequired>={
  name:'rice',
  age:19,
  gender:'男'
}

Readonly

Readonly<Type> 将类型的所有属性设为只读的。

const user: Readonly<IUserInfo> = {
  name: "rice",
  age: 18,
}

//Error:Cannot assign to  age  because it is a read-only property
user.age = 19

Record

Record<Keys,Type> 构造一个对象类型,其属性名的类型为 Keys,属性值的类型为 Type。可用来将某个类型的属性映射到另一个类型上。

type Status = "success" | "fail" | "complete"

const httpStatus: Record<Status, number> = {
  success: 200,
  fail: 400,
  complete: 0,
}

const http2: Record<"a" | "b", string> = {
  a: "1",
  b: "2",
}

上面的 httpStatus 等同于:

interface IStatus {
  success: number
  fail: number
  complete: number
}

const httpStatus: IStatus = {
  success: 200,
  fail: 400,
  complete: 0,
}

Pick

Pick<Type,Keys> 从类型 Type 中选取部分属性 Keys 来构造新的类型。

interface IPicker {
  id: number
  title: string
  content: string
  author: string
}

type PickerType = "id" | "author"

const picker: Pick<IPicker, PickerType> = {
  id: 1,
  author: "rice",
}

Omit

Omit<Type,Keys> 从类型 Type 中获取所有的属性,然后从中去除 Keys 属性后构造一个新的类型。

interface IOmit {
  name: string
  age: number
  gender: string
}

const setOmit: Omit<IOmit, "name" | "age"> = {
  gender: "男",
}

Exclude

Exclude<UnionType,U> 从 联合类型 UnionType 中去除 U 的类型。 和 Omit 的区别:Exclude 用在联合类型上,Omit 用在对象类型或者接口上。

// "b"|"c"
type ExcludeType = Exclude<"a" | "b" | "c", "a">

// "c"
type ExcludeType2 = Exclude<"a" | "b" | "c", "a" | "b">

Extract

Extract<UnionType,U>Exclude<UnionType,U> 功能完全相反,用于提取联合类型 UnionTypeU 的类型,如果不存在提取的类型,就返回 never。

// 'a'
type ExtractType = Extract<"a" | "b" | "c", "a" | "d">

// never
type ExtractType2 = Extract<"a" | "b" | "c", "d">

NonNullable

NonNullable<UnionType> 从联合类型 UnionType 中去除 nullundefined

// 'a'|'b'
type NonNullableType = NonNullable<"a" | undefined | null | "b">

Parameters

Parameters<Type> 由函数类型 Type 的参数来构造一个元祖类型。如果函数没有参数,则返回 空数组[]

type Fn=(a:string,b:number)=>void

//[a:string,b:number]
type ParametersType=Parameters<Fn>

//[]
type ParametersType2=Parameters<()=>void>

//[arg:unknown]
type ParametersType3 = Parameters<<T>(arg: T) => T>

ConstructorParameters

ConstructorParameters<Type>Parameters<Type> 类型。只不过这里的 Type 获取的是 构造函数 的全部参数。若 Type 不是 构造函数 ,则返回 never

//[message?:string|undefined]
type CPType = ConstructorParameters<ErrorConstructor>

ReturnType

ReturnType<Type> 返回函数 Type 的返回值类型,如有多个类型则以联合类型返回。

type fn2=()=>string
type fn3=()=>number|string

// string
type RType=ReturnType<fn2>

//number | string
type RType2=ReturnType<fn3>

InstanceType

InstanceType<Type>ReturnType<Type> 功能类似,只不过这里的 Type 为构造函数类型。

class C {
  a = 1;
  b = 2;
}

// C
type IType = InstanceType<typeof C>; 

ThisParameterType

ThisParameterType<Type> 从函数类型 Type 中提取 this 参数的类型,如果没有则返回 unknown

interface IP{
  a:number
}

function fn(this:IP){}

//IP
type ThisPType=ThisParameterType<typeof fn>

OmitThisParameter

OmitThisParameter<Type> 移除函数中 this 的类型,如果没有声明 this 参数,结果类型就是 Type 。 否则结果就是不带 this 参数的类型。泛型会被忽略。

function fn(this:number){
  return this.toString(16)
}

const str:OmitThisParameter<typeof fn>=fn.bind(5)

//'5'
console.log(str()) 
上次编辑于: