Subject address: Github

Online editor: TypeScript Playground

1. Pick

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

2. Readonly

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

3. Tuple to Object

Provides an array that implements a utility type whose key types are elements of the array

type TupleToObject<T extends readonly any[]> = {
  [K in T[number]]: K
}
Copy the code

In TS, types can also be accessed using indexes. In particular, the related types of an array class can be indexed using an explicit numeric subscript or number, which is used to get all the element types in the array

For details, please read: www.typescriptlang.org/docs/handbo…

// as const is required to solidify the array into a tuple type with a literal type
// This is an explicit literal type for type indexing, otherwise it is a typeof operation on array elements
const arr = ['number'.'string'.'object'] as const;

type ArrFirst = typeof arr[0]; // 'number'
type ArrAll = typeof arr[number]; // 'string' | 'number' | 'object'
Copy the code

So T[number] is the joint literal type of the elements in the tuple, and in is used to get each type

4. First of Array

// My first version of the answer did not consider an empty array, so this answer would be undefined if passed an empty array
// if undefined is also expected, it is not impossible
// type First<T extends any[]> = T[0];

type First<T extends any[]> = T extends[]?never : T[0];
Copy the code

Conditional types are used here, as in ternary expressions. For details, please read: www.typescriptlang.org/docs/handbo…

5. Length of Tuple

// See if you can accept an empty tuple
type Length<T extends readonly any[]> = T['length'];
Copy the code

6. Exclude

Constructs a form that excludes all union members that are assignable to ExcludedUnion. Constructs a type by exclusive from type all union members that are assignable to ExcludedUnion

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

7. Awaited

Get the return value type of the Promise

type Awaited<T> = T extends Promise<infer R> ? R : T;
Copy the code

Infer is a type variable declared within the conditional type. This means that if T can match the Promise

, return R

Resources: jkchao. Making. IO/typescript -…

8. if

The ternary judgment

type If<C extends true | false, T, F> = C extends true ? T : F;
Copy the code

9. Concat

Implement an array-like Concat type

type Concat<Arr1 extends any[], Arr2 extends any[]> = [...Arr1, ...Arr2];
Copy the code

10. Includes

Implement an array-like Includes type

type Includes<Arr extends any[], El> = El extends Arr[number]?true : false;
Copy the code

11. Push

type Push<Arr extends any[], El> = [...Arr, El];
Copy the code

12. Unshift

type Unshift<Arr extends any[], El> = [El, ...Arr];
Copy the code

13. Parameters

type MyParameters<F extends Function> = F extends(... args: infer R) =>any ? R : never;
Copy the code

If there is any mistake, welcome criticism and correction