泛型约束数组时使用 readonly

type A<T extends readonly any[]> = any

因为这样既可以接收 readonly 数组,也可以接收非 readonly 数组:

type A<T extends readonly any[]> = any
type Arr =  [1, 2]
type ReadOnlyArr = readonly [1, 2]
 
const a: A<Arr> = []
const b: A<ReadOnlyArr> = []

如果泛型约束数组时没有使用 readonly,则只能接收非 readonly 数组:

type B<T extends any[]> = any
type Arr =  [1, 2]
type ReadOnlyArr = readonly [1, 2]
 
const a: B<Arr> = []
const b: B<ReadOnlyArr> = [] // 报错:The type 'ReadOnlyArr' is 'readonly' and cannot be assigned to the mutable type 'any[]'