0

如何反转这个“只读”函数元组数组?我在这里typescript-tuple从这个库中借用了 Reverse 。

操场

namespace Utils {
    export type IsFinite<Tuple extends any[], Finite, Infinite> = {
        empty: Finite
        nonEmpty: ((..._: Tuple) => any) extends ((_: infer First, ..._1: infer Rest) => any)
        ? IsFinite<Rest, Finite, Infinite>
        : never
        infinite: Infinite
    }[
        Tuple extends [] ? 'empty' :
        Tuple extends (infer Element)[] ?
        Element[] extends Tuple ?
        'infinite'
        : 'nonEmpty'
        : never
    ]

    export type Prepend<Tuple extends any[], Addend> =
        ((_: Addend, ..._1: Tuple) => any) extends ((..._: infer Result) => any) ? Result : never


    export type Reverse<Tuple extends any[], Prefix extends any[] = []> = {
        empty: Prefix,
        nonEmpty: ((..._: Tuple) => any) extends ((_: infer First, ..._1: infer Next) => any)
        ? Reverse<Next, Prepend<Prefix, First>>
        : never
        infinite: {
            ERROR: 'Cannot reverse an infinite tuple'
            CODENAME: 'InfiniteTuple'
        }
    }[
        Tuple extends [any, ...any[]]
        ? IsFinite<Tuple, 'nonEmpty', 'infinite'>
        : 'empty'
    ]
}

export type ReadFuncs = readonly ((x: any) => any)[]
export type Reverse<Tuple extends any[]> = Utils.Reverse<Tuple>

const example = [() => 'meow'] as const

type v = Reverse<typeof example>

带只读的游乐场

4

0 回答 0