即使有深度限制,如果深度大于9,则TypeScrip会向我发送错误,但我无法理解为什么?类型脚本最大递归次数似乎被限制为50次,因此是否有理由出现以下错误:
Type instantiation is excessively deep and possibly infinite.ts(2589) (property) payload: EditActions<T, 50, []> 推荐答案您可以使用与lib.es2019.array.d.ts随附的lib.es2019.array.d.ts类似的方法来限制递归深度。
type FlatArray<Arr, Depth extends number> = { "done": Arr, "recur": Arr extends ReadonlyArray<infer InnerArr> ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]> : Arr }[Depth extends -1 ? "done" : "recur"];