简体   繁体   English

如何解决此TypeScript错误:“预期0-1个参数,但有2个”

[英]How to resolve this TypeScript error: “Expected 0-1 arguments, but got 2”

I'm writing Node.js code in JavaScript (not TypeScript) but use the TypeScript static analysis tool to check my JavaScript. 我正在用JavaScript(不是TypeScript)编写Node.js代码,但是使用TypeScript静态分析工具来检查我的JavaScript。

I have the following JavaScript code that uses the stampit library: 我有以下使用Stampit库的JavaScript代码:

import stampit from 'stampit'

const Character = stampit({
  props: {
    name: null,
    health: 100
  },
  init({ name = this.name }) {
    this.name = name
  }
})

const Fighter = stampit(Character, { // inheriting
  props: {
    stamina: 100
  },
  init({ stamina = this.stamina }) {
    this.stamina = stamina;    
  },
  methods: {
    fight() {
      console.log(`${this.name} takes a mighty swing!`)
      this.stamina--
    }
  }
})

I've installed the DefinatelyType package for stampit . 我已经为Stampit安装了DefinatelyType软件包

However, I got the following error and it applies to the whole stampit(Character, {...}) function call: 但是,出现以下错误,它适用于整个stampit(Character, {...})函数调用:

Expected 0-1 arguments, but got 2

Any ideas how to resolve this error? 任何想法如何解决此错误? Even just turn it off using TypeScript? 即使只是使用TypeScript将其关闭?

UPDATE: It seems like it's a bug in @types/stampit. 更新:似乎是@ types / stampit中的错误。 It might work with TypeScript without issues, but has issues with writing JS code. 它可以与TypeScript一起使用而没有问题,但是在编写JS代码时有问题。 The issue can be resolved by changing the stampit declaration to: 通过将stampit声明更改为:可以解决此问题:

declare function stampit(f1?: stampit.Stamp | Options, options?: Options): stampit.Stamp;

The type definitions are for stampit v3.0.x , whose most recent release was 3.0.6 two years ago , so (assuming you freshly installed stampit v4.1.2) the definitions are out of sync with the code & documentation. 类型定义是针对Stampit v3.0.x的 ,其最新版本是两年前的3.0.6 ,因此(假设您是刚安装的Stampit v4.1.2)这些定义与代码和文档不同步。 Your options are: 您的选择是:

  1. (strongly discouraged) Use stampit version v3.0.6 ( npm i -S stampit@3.0.6 ) (强烈建议)使用Stampit版本v3.0.6( npm i -S stampit@3.0.6
  2. Write updated type definitions for stampit v4.1.2 (and, hopefully, create a pull request against the DefinitelyTyped repository) 为Stampit v4.1.2编写更新的类型定义(并希望针对DefinitelyTyped存储库创建拉取请求)
  3. Add //@ts-ignore everywhere you get an error, as vibhor1997a said 添加//@ts-ignore到处出现错误,如vibhor1997a所说
  4. Explicitly treat the library as untyped: const stampit: any = require("stampit"); 显式地将库视为无类型的: const stampit: any = require("stampit");

#2 is the best, obviously, since it also benefits anyone else who wants to use the library. 显然,#2是最好的,因为它还会使其他想要使用该库的人受益。 If you don't have the time or inclination to do that, I would advise against #3 because it's error-prone and tedious. 如果您没有时间或意愿这样做,我建议不要使用#3,因为它容易出错且乏味。 That leaves #4, which at least forces you to be explicit and vigilant. 剩下的就是#4,这至少迫使您保持露骨和警惕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM