psiphi75
psiphi75

Reputation: 2065

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.

I have the following JavaScript code that uses the stampit library:

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.

However, I got the following error and it applies to the whole stampit(Character, {...}) function call:

Expected 0-1 arguments, but got 2

Any ideas how to resolve this error? Even just turn it off using TypeScript?

Upvotes: 2

Views: 4823

Answers (1)

Aankhen
Aankhen

Reputation: 2272

UPDATE: It seems like it's a bug in @types/stampit. It might work with TypeScript without issues, but has issues with writing JS code. The issue can be resolved by changing the stampit declaration to:

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. Your options are:

  1. (strongly discouraged) Use stampit version v3.0.6 (npm i -S [email protected])
  2. Write updated type definitions for stampit v4.1.2 (and, hopefully, create a pull request against the DefinitelyTyped repository)
  3. Add //@ts-ignore everywhere you get an error, as vibhor1997a said
  4. Explicitly treat the library as untyped: const stampit: any = require("stampit");

#2 is the best, obviously, since it also benefits anyone else who wants to use the library. If you don’t have the time or inclination to do that, I would advise against #3 because it’s error-prone and tedious. That leaves #4, which at least forces you to be explicit and vigilant.

Upvotes: 3

Related Questions