gpt4 book ai didi

javascript - typescript 从函数中删除第一个参数

转载 作者:行者123 更新时间:2023-12-01 15:52:05 25 4
gpt4 key购买 nike

我有一个可能很奇怪的情况,我正在尝试用 typescript 建模。

我有一堆具有以下格式的函数

type State = { something: any }


type InitialFn = (state: State, ...args: string[]) => void


我希望能够创建一个表示 InitialFn 的类型删除了第一个参数。就像是
// this doesn't work, as F is unused, and args doesn't correspond to the previous arguments
type PostTransformationFn<F extends InitialFn> = (...args: string[]) => void


这可能吗?

最佳答案

我认为您可以以更通用的方式做到这一点:

type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never;

接着:
type PostTransformationFn<F extends InitialFn> = OmitFirstArg<F>;

PG

关于javascript - typescript 从函数中删除第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764853/

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