interface Props { a?: number; b?: string; } const obj: Props = { a: 5 }; const obj2: Required<Props> = { a: 5 }; //Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.
Readonly
构造一个所有属性的Type都设置为readonly的类型,这意味着构造类型的属性不能重新分配
1 2 3 4 5 6 7 8 9 10
interface Todo { title: string; } const todo: Readonly<Todo> = { title: "Delete inactive users", }; todo.title = "Hello"; //Cannot assign to 'title' because it is a read-only property.
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和Union的交集(可以赋值给Union成员的值)
1 2 3 4 5 6
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
1 2 3 4 5 6
type T0 = NonNullable<string | number | undefined>; //type T0 = string | number type T1 = NonNullable<string[] | null | undefined>; //type T1 = string[]
declarefunctionf1(): { a: number; b: string }; 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 extendsnumber[]>() => T>; //type T3 = number[] 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 T7 = any type T8 = ReturnType<Function>; //Type 'Function' does not satisfy the constraint '(...args: any) => any'. // Type 'Function' provides no match for the signature '(...args: any): any'. //type T8 = any