In a mapping type, the new type converts every property of the old type in the same way.

Partial

Convert each property to an optional property

type Partial<T> = {
    [P inkeyof T]? : T[P]; }Copy the code

Example:

type PersonPartial = Partial<Person>;
// ^ = type PersonPartial = {
// name? : string | undefined;
// age? : number | undefined;
/ /}
Copy the code

Readonly

Convert each property to a read-only property

type Readonly<T> = {
    readonly [P in keyof T]: T[P];
}
Copy the code

Example:

type ReadonlyPerson = Readonly<Person>;
// ^ = type ReadonlyPerson = {
// readonly name: string;
// readonly age: number;
/ /}
Copy the code

Nullable

Converts to the associative type of the old type and null

type Nullable<T> = { 
  [P in keyof T]: T[P] | null 
}
Copy the code

Example:

type NullablePerson = Nullable<Person>;
// ^ = type NullablePerson = {
// name: string | null;
// age: number | null;
/ /}
Copy the code

Pick

Select a set of properties to specify a new type

type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
}
Copy the code

Example:

interface Todo {
  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

Record

Create a set of properties to specify a new type, often used to declare ordinary Object objects

type Record<K extends keyof any, T> = {
  [P in K]: T;
}
Copy the code

Record is a non-homomorphism, which essentially creates a new property and does not copy the property modifier.

Example:

interface PageInfo {
  title: string;
}

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

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

Exclude

Remove the intersection and return the remainder

type Exclude<T, U> = T extends U ? never : T
Copy the code

Example:

interfaceProps { 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>'.
Copy the code

Omit

Exclude, applied to a key-value pair, excludes the key-value pairs contained in a type

type Omit = Pick<T, Exclude<keyof T, K>>
Copy the code

Example:

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

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

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

ReturnType

Gets the return value type, usually a function

type ReturnType<T extends(... args:any) = >any>
  = T extends(... args:any) => infer R ? R : any;
Copy the code

Example:

declare function f1() :{ a: number; b: string };
type T4 = ReturnType<typeof f1>;
// ^ = type T4 = {
// a: number;
// b: string;
/ /}
Copy the code

Required

Convert each attribute to a mandatory attribute

type Required<T> = {
  [P inkeyof T]-? : T[P] }Copy the code

Example:

interfaceProps { 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>'.
Copy the code

There are many similar mapping types, but see TypeScript: Documentation for more detailed information.