Partial<T>
将T的所有类型转换为可选类型,返回的类型可以为T的所有子集
interface Todo { title: string; description: string; } type subTodo = Partial<Todo> const todo1: subTodo = { title: 'This is a title' } const todo2: subTodo = { description: 'This is a description' }
interface Todo { title?: string; description?: string; } const todo1: Todo = { title: 'This is a title' } const todo2: Todo = { description: 'This is a description' } const todo3: Required<Todo> = { // 'description' is declared here. title: '123' }
interface Todo { title: string; } const todo: Readonly<Todo> = { title: 'This is a title' } todo.title = 'new title' // Cannot assign to 'title' because it is a read-only property.
interface Todo { title: string; } type TodoList = 'todo1' | 'todo2' const todos: Record<TodoList, Todo> = { todo1: { title: 'todo1' }, todo2: { title: 'todo2' } }
interface Todo { title: string; description: string; done: boolean; } type TodoPreview = Pick<Todo, 'title' | 'done'> const todo: TodoPreview = { title: 'title', done: true }
interface Todo { title: string; description: string; done: boolean; } type TodoPreview = Omit<Todo, 'description' | 'done'> const todo: TodoPreview = { title: 'title' }
type Todo = string | number | (() => void) type TodoPreview = Exclude<Todo, string> // type TodoPreview = number | (() => void)
type Todo = string | number | (() => void) type TodoPreview = Extract<Todo, string> // type TodoPreview = string
type Todo = NonNullable<number | string | null | undefined> // tyoe Todo = <number | string>
type T0 = Parameters<() => string> // type T0 = [] type T1 = Parameters<(s: string) => void> // type T1 = [s: string]
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 T0 = ReturnType<() => string> // type T0 = string
class C { x = 0; y = 0; } type T0 = InstanceType<typeof C>; // C type T1 = InstanceType<any>; // any type T2 = InstanceType<never>; // any