Review
- 2024-07-21 21:19
一、Introduction #
Awaited<Type>This type is meant to model operations like await in async functions, or the.then()method on Promises - specifically, the way that they recursively unwrap Promises.Partial<Type>: makes all properties of a type optional.Required<Type>: Constructs a type consisting of all properties ofTypeset to required. The opposite ofPartial.Readonly<Type>: makes all properties of a type read-only.Record<Keys, Type>:Recordconstructs an object type whose property keys areKeysand whose property values areType. This utility can be used to map the properties of a type to another type.Pick<Type, Keys>: allows you to pick specific properties from a type.Omit<Type, Keys>: allows you to omit specific properties from a type.Exclude<UnionType, ExcludedMembers>: Constructs a type by excluding fromUnionTypeall union members that are assignable toExcludedMembers. creates a type that is the set difference of A and B. 差集Extract<Type, Union>Extract constructs a type by extracting fromTypeall union members that are assignable toUnion. 交集NonNullable<Type>Non-Nullable constructs a type by excludingnullandundefinedfrom Type.Parameters<Type>Parameters constructs a tuple type from the types used in the parameters of a function typeType.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 typeneverifTypeis not a function).ReturnTypeReturn type constructs a type consisting of the return type of function Type.InstanceTypeThis type constructs a type consisting of the instance type of a constructor function in Type.NoInfer<Type>Blocks inferences to the contained type. Other than blocking inferences,NoInfer<Type>is identical toType.ThisParameterType<Type>Extracts the type of the this parameter for a function type, orunknownif the function type has nothisparameter.OmitThisParameter<Type>Removes thethisparameter fromType. IfTypehas no explicitly declaredthisparameter, the result is simplyType. Otherwise, a new function type with nothisparameter is created fromType. Generics are erased and only the last overload signature is propagated into the new function type.ThisType<Type>This utility does not return a transformed type. Instead, it serves as a marker for a contextualthistype. Note that thenoImplicitThisflag must be enabled to use this utility.Uppercase<StringType>Lowercase<StringType>Capitalize<StringType>Uncapitalize<StringType>
interface User {
name: string;
age: number;
email: string;
}Partial<Type>
#
Partial<User>Pick<User, 'name' | 'age'>Omit<User, 'age' | 'email'>Readonly<User>interface CatInfo {
age: number;
breed: string;
}
type CatName = 'miffy' | 'boris' | 'mordred';
const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: 'Persian' },
boris: { age: 5, breed: 'Maine Coon' },
mordred: { age: 16, breed: 'British Shorthair' },
};Exclude
#
type T0 = Exclude<'a' | 'b' | 'c', 'a'>; // "b" | "c"
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>; // "c"
type T2 = Exclude<string | number | (() => void), Function>; // string | number
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; x: number }
| { kind: "triangle"; x: number; y: number };
type T3 = Exclude<Shape, { kind: "circle" }>type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>;
// ^ = type T0 = "a"
type T0 = NonNullable<string | number | undefined>;
// type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
// type T1 = string[]
Parameters
#
type T0 = Parameters<() => string>;
// type T0 = []
type T1 = Parameters<(s: string) => void>;
// type T1 = [s: string]
type T2 = Parameters<<T>(arg: T) => T>;
// type T2 = [arg: unknown]
declare function f1(arg: { a: number; b: string }): void;
type T3 = Parameters<typeof f1>;
// type T3 = [arg: {
// a: number;
// b: string;
// }]
type T4 = Parameters<any>;
// type T4 = unknown[]
type T5 = Parameters<never>;
// type T5 = never
type T6 = Parameters<string>;
// ^ Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T7 = Parameters<Function>;
// ^ Type 'Function' does not satisfy the constraint '(...args: any) => any'.
ConstructorParameters<Type>
#
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]
class C {
constructor(a: number, b: string) {}
}
type T3 = ConstructorParameters<typeof C>;
// type T3 = [a: number, b: string]
type T4 = ConstructorParameters<any>;
// type T4 = unknown[]
type T5 = ConstructorParameters<Function>;
// type T5 = never
ReturnType<Type>
#
type T0 = ReturnType<() => string>;
// type T0 = string
type T1 = ReturnType<(s: string) => void>;
// type T1 = void
type T2 = ReturnType<<T>() => T>;
// type T2 = unknown
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
// type T3 = number[]
declare function f1(): { a: number; b: string };
type T4 = ReturnType<typeof f1>;
// type T4 = {
// a: number;
// b: string;
// }
type T5 = ReturnType<any>;
// type T5 = any
type T6 = ReturnType<never>;
// type T6 = never
type T7 = ReturnType<string>;
// ^ Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T8 = ReturnType<Function>;
// ^ Type 'Function' does not satisfy the constraint '(...args: any) => any'.
InstanceType<Type>
#
class C {
x = 0;
y = 0;
}
type T0 = InstanceType<typeof C>;
// type T0 = C
type T1 = InstanceType<any>;
// type T1 = any
type T2 = InstanceType<never>;
// type T2 = never
type T3 = InstanceType<string>;
// ^ Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
type T4 = InstanceType<Function>;
// ^ Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
type A = Awaited<Promise<string>>;
// type A = string
type B = Awaited<Promise<Promise<number>>>;
// type B = number
type C = Awaited<boolean | Promise<number>>;
// type C = number | boolean
NoInfer<Type>
#
function createStreetLight<C extends string>(
colors: C[],
defaultColor?: NoInfer<C>,
) {
// ...
}
createStreetLight(["red", "yellow", "green"], "red"); // OK
createStreetLight(["red", "yellow", "green"], "blue"); // Error
ThisParameterType<Type>
#
function toHex(this: Number) {
return this.toString(16);
}
function numberToString(n: ThisParameterType<typeof toHex>) {
return toHex.apply(n);
}type LowercaseGreeting = "hello, world";
type Greeting = Capitalize<LowercaseGreeting>;
// type Greeting = "Hello, world"
type UppercaseGreeting = "HELLO WORLD";
type UncomfortableGreeting = Uncapitalize<UppercaseGreeting>;
// type UncomfortableGreeting = "hELLO WORLD"