Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Preface:

If you want to start from scratch, please start from the first one juejin.cn/post/701033…

The first question

Implements an Includes tool type that determines whether the specified type E is included in the T array type. The specific usage example is as follows:

Type I0 = Includes<[], 1> // False type I1 = Includes<[2, 2, 3, 1] 2> // true type I2 = Includes<[2, 3, 3, 1], 1> // trueCopy the code

Investigate knowledge points

  1. How do I convert an array to an associative type
  2. Application of recursion

Method a

The array is converted to the union type

type Includes<T extends Array<any>, E> = E extends T[number] ? true :false type I0 = Includes<[], 1> // false type I1 = Includes<[2, 2, 3, 1], 2> // true type I2 = Includes<[2, 3, 3, 1], 1> // trueCopy the code

Array [number] = array[number] = array[number]

Method 2

Use recursion, compare one by one, exit condition is found to be the same, and compare end

type IsEqual<A, B> = A extends B? B extends A ? true :false :false type Includes<T extends Array<any>, E> = T extends[first : infer A , ...rest :infer B ] ? IsEqual<A ,E> extends true ? true :Includes<B,E> : false type I0 = Includes<[], 1> // false type I1 = Includes<[2, 2, 3, 1], 2> // true type I2 = Includes<[2, 3, 3, 1], 1> // trueCopy the code

Summary:

  1. Let’s first define a way to determine equality.
  2. If the input parameter is an empty array, the comparison fails and false is returned.
  3. Infer is then used to compare the first item of the array with the specified type. On success, true is returned, and on failure, the remaining parameters are recursively compared as input parameters.

The idea of this solution is to focus on the recursive end condition, successful end, the array is empty end.

The second question

Implements a UnionToIntersection tool type that converts a union type to an intersection type. The specific usage example is as follows:

Type UnionToIntersection < U > = / / your implementation code / / test case type U0 = UnionToIntersection < string | number > / / never type U1 = UnionToIntersection<{ name: string } | { age: number }> // { name: string; } & { age: number; }Copy the code

Investigate knowledge points

By using the inverse function parameter type, the transformation from the joint type to the cross type is realized. When the parameter of the function is the joint type, it will be changed to the cross type

Answer key:

// Implements a UnionToIntersection tool type that converts a union type to a crossover type. An example is as follows: Type UnionToIntersection<U> = (U extends U? (arg: U) => any: never) extends (arg: infer T) => any ? T : never; / / test case type U0 = UnionToIntersection < string | number > / / never type U1 = UnionToIntersection < {name: string} | {age: number }> // { name: string; } & { age: number; }Copy the code

Summary:

Use incoming parameters as function parameters and infer the values