gpt4 book ai didi

typescript :如何将项目添加到元组?

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:14 24 4
gpt4 key购买 nike

为什么这会产生错误“A rest element type must be an array type.”?

type QWE<T extends [number, string]> = [boolean, ...T]
^^^^ the error is here

最佳答案

如果有人觉得它有用,这是 Titian 类型的一个版本,改进了更多情况:

type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]


type TupleUnshift<A, B extends [...any[]]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never

type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T

关于 typescript :如何将项目添加到元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com