logoProsperBao

Push

2022-03-22 04

题目来源(type-challenges)

问题

在类型系统里实现通用的 Array.push

举例如下,

type Result = Push<[1, 2], '3'> // [1, 2, '3']

解答

type Push<T extends unknown[], U> = [...T, U]

拆分

  1. T extends unknown[] 限制 T 只能是数组类型
  2. 通过解构加入 U 形成新的类型