TS Advanced Types: Advanced Types

Required<Type>

Construct a Type that consists of all the attributes of Type set to required. Use Partial instead

interfaceProps { a? :number; b? :string;
}

const obj: Props = { a: 5 };

const obj2: Required<Props> = { a: 5 };
Copy the code

In '{a: number; }' attribute 'b' can be missing, but in 'Required<Props>' attribute 'b' is Required;

Partial<Type>

Construct a type type that is optional with all properties of type. It returns the Type of a subset of all attributes of the given Type;

interface Todo {
  title: string;
  description: string;
}

function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
  return{... todo, ... fieldsToUpdate }; }const todo1 = {
  title: "organize desk".description: "clear clutter"};const todo2 = updateTodo(todo1, {
  description: "throw out trash"});Copy the code

Readonly<Type>

Construct a type of type where all properies properties are set to read-only, meaning that all properties of that type cannot be assigned.

interface Todo {
  title: string;
}

const todo: Readonly<Todo> = {
  title: "Delete inactive users"}; todo.title ="Hello";
Copy the code

You cannot assign a value to the title property because it is read-only.

Record<Keys,Type>

Construct a kind of object type, its attribute key value is keys, corresponding value value is type, it can be used to map the attribute of one type to another type;

interface PageInfo {
  title: string;
}

type Page = "home" | "about" | "contact";

const nav: Record<Page, PageInfo> = {
  about: { title: "about" },
  contact: { title: "contact" },
  home: { title: "home"}}; todo;// ^ = const todo: TodoPreview
Copy the code

Pick<Type, Keys>

Construct a type, Type, by selecting a set of attributes Keys in Type

  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;

const todo: TodoPreview = {
  title: "Clean room".completed: false}; todo;// ^ = const todo: TodoPreview
Copy the code

Omit<Type, Keys>

Construct a type, Type, by selecting all the properties in Type and removing Keys

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Omit<Todo, "description">;

const todo: TodoPreview = {
  title: "Clean room".completed: false}; todo;// ^ = const todo: TodoPreview
Copy the code

Exclude<Type, ExcludedUnion>

Construct a type type by excluding all union members of type type assigned to the 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
Copy the code

Extract<Type, Union>

Construct a type type by extracting all Union members of type type assigned to the Union;

type T0 = Extract<"a" | "b" | "c"."a" | "f">;
// ^ = type T0 = "a"
type T1 = Extract<string | number | (() = > void), Function>;
// ^ = type T1 = () => void
Copy the code