TypeScript provides built-in utility types that make it easier to convert types from one form to another. These built-in types are available globally, so you can easily use them.

Typescript generic

Type aliases and generics are important before we get to know TypeScript’s utility and types. We create a type alias in TypeScript for any existing type.

  type MyString = string;

  let helloWorldMessage: MyString = 'Hello Wisdom Geek';
Copy the code

Generics are used to create reusable type aliases. Suppose we have an identity function that returns any value passed:

  const identity = (arg: string) :string= > arg;
Copy the code

What if we want to return a number? Some friends might use any instead of a particular type:

  const identity = (arg: any) :any= > arg;
Copy the code

But this reduces the type information of arguments and, therefore, loses the benefits of TypeScript. We want to capture the type of the parameter in a way that can be used to represent the return type. This is where generics come in handy. We will use type variables that apply to types, not values.

  const identity<T> = (arg: T): T= > arg;
Copy the code

Next, specify the type of the function when calling it:

  const output = identity<string> ("Hello Wisdom Geek");
Copy the code

Built-in utility types in TypeScript

  • Partial

    Pritial makes all properties of T optional.

 type BlogPost = {
   title: string;
   author: string;
 }

 type PartialBlogPost = Partial<BlogPost>;
 /* equivalent to {title? : string; author? : string; } *Copy the code
  • Required Makes all attributes of T Required.
  typePartialBlogPost = { title? :string; author? :string;
  }

  type BlogPost = Required<PartialBlogPost>;
  /* 等价于 {
    title: string;
    author: string;
  } */

Copy the code
  • Readonly Readonly makes all properties of T read-only.

      type BlogPost = {
        title: string;
        author: string;
      }
    
      type BlogPost = Readonly<PartialBlogPost>;
    
      /* 等价于 {
        readonly title: string;
        readonly author: string;
      } */
    Copy the code
  • Pick Pick

    extract the attributes in T from K.
    ,k>

       type Point3D = {
        x: number.y: number.z: number};type Point2D = Pick<Point3D, 'x'|'y'>;
       /* equivalent to {x: number, y: number} */
    Copy the code
  • Parameters

Parameters T is a Function that returns a tuple.

	type T0 = Parameters<() = > string>;
	// type T0 = []

	type T1 = Parameters<(s: string) = > void>; 
	// type T1 = [s: string]

	type T2 = Parameters<<T>(arg: T) = > T>;
	// type T2 = [arg: unknown]


Copy the code
  • Omit

Omit<T,K> as opposed to Pick (remove attributes).

  type Point3D = {
    x: number.y: number.z: number};type Point2D = Omit<Point3D, 'z'>;
  /* same as { x: number, y: number } */
Copy the code
  • Record

Record<K,T> generates an interface with attributes for all properties of K, and all properties of K have type T

  type BlogPost = Record<'title' | 'author', strnig>

  /* same as { title: string; author: string; } * /
Copy the code

If all types have the same value, the declared version of Record is more concise and readable because they all have the same type.

  • Extract

    – Used to Extract members from type T that can be assigned to type U
    ,>
  type T0 = Extract<"a" | "b" | "c"."a" | "f">;
   // type T0 = "a"
  type T1 = Extract<string | number | (() = > void), Function>;  
   // type T1 = () => void
Copy the code
  • Exclude

Exclude<T, U> – Removes members from type T that are not in type U.

    type T0 = Exclude<"a" | "b" | "c"."a">;
    // type T0 = "b" | "c"

    type T1 = Exclude<string | number | (() = > void), Function>;
    // type T2 = string | number
Copy the code
  • NonNullable

NonNullable- Used to remove undefined and null from type T.

   type T0 = NonNullable<string | number | undefined>;
   // type T0 = string | number

   type T1 = NonNullable<string[] | null | undefined>;
   // type T1 = string[]
Copy the code
  • ReturnType ReturnType- gets the ReturnType of the function type
   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 extends number[] >() = > T>;
   
   type T3 = number[]

   type T5 = ReturnType<any>;
   
   type T5 = any
   type T6 = ReturnType<never>;
   
   type T6 = never
   type T7 = ReturnType<string>;
Copy the code
  • InstanceType

InstanceType- gets the InstanceType of the constructor

   class C {
     x = 0;
     y = 0;
   }

   type T0 = InstanceType<typeof C>;
   
   type T0 = C
   type T1 = InstanceType<any>;
   
   type T1 = any
   type T2 = InstanceType<never>;
   
   type T2 = never 
Copy the code

Refer to the website

www.typescriptlang.org/docs/handbo…

Welcome to my blog: juejin.cn/user/246754…

Github address: github.com/NurTuam

Lots of support! I will continue to update ❤️