打字稿:数组类型取决于前一个数组元素

时间:2020-12-19 18:39:25

标签: arrays typescript generics types

是否可以在 Typescript 中实现一个无限数组(不是元组),其类型取决于数组的前一个元素?

这里有一些伪打字稿代码来举例:

class B<T, U> {}

function foo<X, Y>(...args: [B<X, Z0>, B<Z0, Z1>, B<Z1, Z2>, ..., B<ZN, Y>]) {}

foo<string, number>(new B<string, number>, new B<number, boolean>, new B<boolean, number>); // Correct
foo<string, number>(new B<string, number, new B<number, boolean>); // Incorrect
foo<string, number>(new B<string, number>, new B<boolean, number>); // Incorrect

我应该用什么来替换“[B<X, Z0>, B<Z0, Z1>, B<Z1, Z2>, ..., B<ZN, Y>]”以使其正常工作?这甚至可能吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我最近遇到了类似的问题。我的问题涉及创建一条长管道。流 i 的输出必须是流 i+1 等的输入。我是这样解决的:

type OmitFirst<T extends any[]> = T extends [any, ...infer R] ? R : never
type GetValueOrDefault<Thing, Key, Default = never> = Key extends keyof Thing ? Thing[Key] : Default
type Pipe<Start, Finish, Inputs extends [Start, ...any[]], Outputs extends any[] = OmitFirst<Inputs>> = {
    [I in keyof Inputs]: DuplexConstructor<Inputs[I], GetValueOrDefault<Outputs, I, Finish>>
}

export function pipe<F, L, P extends [F, ...any[]]>(
    ...streams: [ReadableConstructor<F>, ...Pipe<F, L, P>, WritableConstructor<L>]
): Writable {
    const source = streams[0]
    const duplexes = streams.slice(1, -1) as Pipe<F, L, P>
    const sink = streams[streams.length - 1] as WritableConstructor<L>

    let tail = readable(source)
    duplexes.forEach((dp) => (tail = tail.pipe(duplex(dp))))
    return tail.pipe(writable(sink))
}