This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

1.Partial<Type>: Make properties of a type “optional”

Type Partial<T> = {[P in keyof T]? : T[P]; };Copy the code
  • Index type query operator: keyOF
type Partial<T> = { [P in keyof T]? : T[P]; / / Make the key in each pair optional. }Copy the code
  • use
interface User { name: string age: number department: String} type optional = Partial<User> type optional = {name? : string | undefined; age? : number | undefined; department? : string | undefined; }Copy the code

2.Required<Type>: Makes properties of a type mandatory

Type Required<T> = {[P in keyof T]-? : T[P]; }; // The minus sign is the removal conditionCopy the code
  • use
interface Props { a? : number; b? : string; } const obj: Props = { a: 5 }; const obj2: Required<Props> = { a: 5 }; / / an errorCopy the code

3.Readonly<Type>: makes a member of type T read-only

Type Readonly<T> = {Readonly [P in keyof T]: T[P]; };};Copy the code
  • use
interface Todo { title: string; } const todo: Readonly<Todo> = { title: "Delete inactive users", }; todo.title = "Hello"; // If the property is read-only, an error is reportedCopy the code

4.Record: Map all property values of one type to another type and create a new type

Type Record<K extends string, T> = [P in K]: T; }Copy the code
  • use
interface PageInfo { title: string; } type Page = "home" | "about" | "contact"; const nav: Record<Page, PageInfo> = { about: { title: "about" }, contact: { title: "contact" }, home: { title: "home" }, }; / / Record at the back of the generic is key and the value of the object types have # ABC three properties, the value of the attribute must be A numeric type keys = 'A' | 'B' | 'C' const result: Record < keys, number > = {A: 1, B: 2, C: 3 }Copy the code

5.Pick<Type, Keys>: Takes a combination of several desired types from a compound type

Type Pick<T, K extends keyof T> = {[P in K]: T[P]; };Copy the code
  • use
interface Todo {
  title: string;
  description: string;
  completed: boolean;
}
type TodoPreview = Pick<Todo, "title" | "completed">;

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

6.Omit<Type, Keys>: Removes certain properties from another object type and creates a new object type

Pick<T, Exclude<keyof T, K>Copy the code
  • use
type UserProps = { name? :string; age? :number; sex? :string; } type NewUserProps = Omit<UserProps,'sex'> type NewUserProps = {name? :string; age? :number; }Copy the code

7.ReturnTypeTo get the return value type of a function

declare function f1(): { a: number; b: string };

type T0 = ReturnType<() => string>;
  type T0 = string

type T4 = ReturnType<typeof f1>;
  type T4 = { a: number; b: string; }
Copy the code

8.Exclude<Type, ExcludedUnion>: Removes the ExcludedUnion attribute from Type

type T0 = Exclude<"a" | "b" | "c", "a">; / / remove the Type containing the attribute of "a" Type T0 = "b" | "c"Copy the code

9.Extract<Type, Union>:TypeandUnionBoth exist

type T0 = Extract<"a" | "b" | "c", "a" | "f">;
  type T0 = "a"
Copy the code

reference

  • utility-types