【问题标题】:Typescript ESLint error for prefer-rest-params首选休息参数的打字稿 ESLint 错误
【发布时间】:2020-11-18 19:55:08
【问题描述】:

我正在尝试将 Segment.io 分析 sn-p 集成到 Typescript 项目中。

遇到 ESLint 错误,我无法解决。代码如下:

analytics.factory = function (method: any) {
  return function () {
    const args = Array.prototype.slice.call(arguments)
    args.unshift(method)
    analytics.push(args)
    return analytics
  }
}

错误是:

error  Use the rest parameters instead of 'arguments'  prefer-rest-params

我的 Typescript 有点生锈,我尝试了以下方法无济于事:

  • Array.prototype.slice.call(this, ...arguments)
  • Array.prototype.slice.call(null, ...arguments)

非常感谢任何帮助!

【问题讨论】:

标签: typescript eslint typescript-eslint


【解决方案1】:

对于遇到类似问题的任何人,这里是解决方法:

analytics.factory = function(method: any) {
  return function(...args: any[]) {
    args.unshift(method);
    analytics.push(args);
    return analytics;
  };
};

【讨论】:

  • 您不需要需要 const args = Array.prototype.slice.call(argz); - 它只是将argz 转换为一个数组,而argz 已经 是一个数组。删除该行并将定义重命名为function(...args: any[])。同样,该规则清楚地解释了什么是有效的,什么是无效的,所以我不知道你从哪里得到这个代码。
  • 按建议更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多