【问题标题】:TS: Typing higher order function with rest params ...argsTS:使用其余参数键入高阶函数 ...args
【发布时间】:2021-11-04 09:13:57
【问题描述】:

我在 Typescript 中编写了一个小辅助函数,但收到错误消息 A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)

export type TDemethodise = (f: Function) => (...args: any[]) => void

export const demethodise: TDemethodise = (fn) => (...args): void => fn.bind(...args);

我很不确定是否要输入这个。是否可以在不大量更改功能的情况下键入它?

【问题讨论】:

    标签: typescript args spread-syntax


    【解决方案1】:

    Function.bind()thisArg 作为其第一个参数。从您的代码中并不完全清楚您期望 this 在“去方法化”函数中绑定到什么,但例如将其绑定到自身 fn.bind(fn, ...args) 会起作用。

    【讨论】:

      猜你喜欢
      • 2019-09-22
      • 1970-01-01
      • 2018-11-08
      • 2016-03-12
      • 2021-12-30
      • 2023-04-06
      • 2016-08-20
      • 2020-11-11
      • 2016-02-24
      相关资源
      最近更新 更多