一起学一下Typescript的Utility Types
emmm 写写?(用人话翻译文档)
Partial
将Type
的所有属性都置为可选。返回Type
的子集类型
interface User {
name: string
age: number
}
const user: User = {
name: 'yy',
age: 18
}
function updateUserInfo(userInfo: Partial<User>) {
return Object.assign(user, userInfo)
}
Required
将Type
的所有属性都设置为必选。和Partial
的功能正好相反
interface User {
id?: number
name: string
age: number
}
const user: Partial<User> = {}
function createUser(userInfo: Required<User>) {
return Object.assign(user, userInfo)
}
createUser({
name: 'yy',
age: 18
})
// error: Property 'id' is missing in type '{ name: string; age: number; }' but required in type 'Required<User>'.
Readonly
将Type
的所有属性都设置为只读。ts会在你修改类型为Type
的变量属性时报错。
interface User {
id?: number
name: string
age: number
}
const user: Readonly<User> = {
name: 'yy',
age: 18
}
function updateUserAttr(key: keyof User, value: string | number) {
user[key] = value
// error: Cannot assign to 'title' because it is a read-only property.
}
updateUserAttr('name', 'ym')
Record<Keys,Type>
生成一个对象类型,Keys
表示对象key的类型,Type表示对象value
的类型。Keys
和Type
都可以是单一类型或联合类型。
interface User {
id?: number
name: string
age: number
}
const user1: Record<string, string | number> = {}
const userMapping: Record<string, User> = {}
Pick<Type, Keys>
从Type
的属性中挑出Keys
组成新的类型。
interface People {
id?: number
name: string
age: number
sex: number
}
const cat: Pick<Pople, 'name'> = {
name: 'tom'
}
Omit<Type, Keys>
从Type
的属性中剔除Keys
,用剩余属性组成新的类型。
interface People {
id?: number
name: string
age: number
balance: number
}
// 剔除balance属性
const cat: Omit<Pople, 'balance'> = {
name: 'tom'
}
Exclude<Type, ExcludedUnion>
取Type
和ExcludedUnion
类型的差集,组成新类型。
type T0 = Exclude<"a" | "b" | "c", "a">;
// 类型: type T0 = "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
// 类型: type T1 = "c"
type T2 = Exclude<string | number | (() => void), Function>;
// 类型: type T2 = string | number
Extract<Type, Union>
取Type
和ExcludedUnion
类型的交集,组成新类型。
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// 类型: type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;
// 类型: type T1 = () => void
NonNullable
排除Type
联合类型中的null
和undefined
返回一个新的类型。
type T0 = NonNullable<string | number | undefined>;
// 类型: type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
// 类型: type T1 = string[]
Parameters
获取一个函数的参数类型。
interface User {
name: string
age: number
}
declare function createUser(user: User): boolean;
type CreateUser = Parameters<createUser>
// CreateUser的类型是
ConstructorParameters
获取构造函数的参数类型。返回一个所有参数类型组成的数组,数组内类型的顺序和参数的顺序一致。
type T0 = ConstructorParameters<ErrorConstructor>;
// 类型: type T0 = [message?: string]
type T1 = ConstructorParameters<FunctionConstructor>;
// 类型: type T1 = string[]
type T2 = ConstructorParameters<RegExpConstructor>;
// 类型: type T2 = [pattern: string | RegExp, flags?: string]
type T3 = ConstructorParameters<any>;
// 类型: type T3 = unknown[]
type T4 = ConstructorParameters<Function>;
// error: Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
// Type 'Function' provides no match for the signature 'new (...args: any): any'.
// 类型: type T4 = never
ReturnType
创建一个以函数返回值为类型的类型。(这描述是真绕嘴)
declare function createUser(user: Usre): User;
type ResCreateUser = ReturnType<createUser>
// 类型: type ResCreateUser = User
InstanceType
构造一个由构造函数实例组成的类型Type
class User {
name: string
age: number
}
type UserI = InstanceType<typeof User>
// 类型: type UserI = User
Uppercase
返回一个全大写字母的StringType
类型
type Up = Uppercase<'abc'>
// 类型: type Up = ABC
type Up = Uppercase<'aBc'>
// 类型: type Up = ABC
Lowercase
返回一个全小写字母的StringType
类型
type Lw = Lowercase<'abc'>
// 类型: type Lw = abc
type Lw = Lowercase<'aBc'>
// 类型: type Lw = abc
type Lw = Lowercase<'ABC'>
// 类型: type Lw = abc
Capitalize
返回单词首字母大写的StringType
类型
type Cp = Capitalize<'abc'>
// 类型: type Cp = Abc
type Lw = Capitalize<'aBc'>
// 类型: type Cp = Abc
type Lw = Capitalize<'ABC'>
// 类型: type Cp = Abc
type Lw = Capitalize<'ABC'>
// 类型: type Cp = Abc
Uncapitalize
返回单词首字母反转(大写转小写, 小写转大写)的StringType
类型
type Cp = Capitalize<'abc'>
// 类型: type Cp = Abc
type Lw = Capitalize<'ABc'>
// 类型: type Cp = aBc
type Lw = Capitalize<'ABC'>
// 类型: type Cp = aBC
type Lw = Capitalize<'AbC'>
// 类型: type Cp = aBC