reference 其他
# Utility Types 实用类型
Partial<Type>
Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets of a given type. 构造具有 Type 的所有属性设置为可选的类型。此实用程序将返回表示给定类型的所有子集的类型。
Required<Type>
Constructs a type consisting of all properties of Type set to required. The opposite of Partial. 构造一个包含 Type 设置为 required 的所有属性的类型。与 Partial 相反。
Readonly<Type>
Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned. 构造具有 Type 的所有属性设置为只读的类型,这意味着不能重新分配构造类型的属性。
Record<Keys, Type>
Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type. 构造属性键为 Keys、属性值为 Type 的对象类型。此实用工具可用于将一个类型的属性映射到另一个类型。
Pick<Type, Keys>
Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type. 通过从 Type 中选择一组属性 Keys (字符串文本或字符串文本的并集) 来构造类型。
Omit<Type, Keys>
Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals). 通过从 Type 中选取所有属性,然后移除 Keys (字符串文字或字符串文字的并集) 来构造类型。
Exclude<Type, ExcludedUnion>
Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion. 通过从类型中排除可分配给 ExcludedUnion 的所有联合成员来构造类型。
Extract<Type, Union>
Constructs a type by extracting from Type all union members that are assignable to Union. 通过从 Type 中提取可分配给 Union 的所有联合成员来构造类型。
NonNullable<Type>
Constructs a type by excluding null and undefined from Type. 通过从 Type 中排除 null 和 undefined 来构造类型。
Parameters<Type>
Constructs a tuple type from the types used in the parameters of a function type Type. 从函数类型类型的参数中使用的类型构造元组类型。
ConstructorParameters<Type>
Constructs a tuple or array type from the types of a constructor function type. It produces a tuple type with all the parameter types (or the type never if Type is not a function). 从构造函数类型的类型构造元组或数组类型。它生成具有所有参数类型的元组类型 (如果 Type 不是函数,则生成 never 类型)。
ReturnType<Type>
Constructs a type consisting of the return type of function Type. 构造一个类型,包含函数 Type 的返回类型。
InstanceType<Type>
Constructs a type consisting of the instance type of a constructor function in Type. 构造由 Type 中构造函数的实例类型组成的类型。
ThisParameterType<Type>
Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter. 提取函数类型的此参数的类型,如果函数类型没有此参数,则未知。
OmitThisParameter<Type>
Removes the this parameter from Type. If Type has no explicitly declared this parameter, the result is simply Type. Otherwise, a new function type with no this parameter is created from Type. Generics are erased and only the last overload signature is propagated into the new function type. 从 Type 中移除此参数。如果 Type 没有显式声明此参数,则结果只是 Type。否则,将从 Type 创建没有此参数的新函数类型。泛型被擦除,只有最后一个重载签名被传播到新的函数类型中。
function toHex(this: Number) {
return this.toString(16);
}
const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);
console.log(fiveToHex());
2
3
4
5
6
7
ThisType<Type>
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type. Note that the noImplicitThis flag must be enabled to use this utility. 此实用程序不返回转换后的类型。相反,它可以作为这种类型上下文的标记。请注意,必须启用 noImplicitThis 标志才能使用此实用程序。
- Intrinsic String Manipulation Types
Uppercase<StringType>
Lowercase<StringType>
Capitalize<StringType>
Uncapitalize<StringType>