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

标签 javascript typescript generics

我有一个可能很奇怪的情况,我正在尝试用 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/

相关文章:

javascript - Html:修复文本字段中的空格

javascript - 模块混淆和重构 JS/jQuery

javascript - 力向图 - 定义节点的颜色,如果该节点同时是源和目标

typescript - 如何将 DynamoDB AttributeMap 类型映射到接口(interface)?

Typescript:空检查在 array.map 函数内不起作用

generics - IDictionary<Tkey, Tvalue> 的通用方法 - 找不到类型或 namespace ?

xcode - Swift:是否可以为 UIViewController 使用通用和 Storyboard

javascript - 使用 javascript 访问最后一个 JSON 元素

c# - 通用函数和值类型

javascript - 'null coalescing' TS 友好函数