1

I am trying to call a super method where the definitions are the same as this method.

I call it like this:

return await super.chunk(...args)

However, it gives me this error:

Expected 2-3 arguments, but got 0 or more.

So, I tried to type the value passed in like this:

return await super.chunk(...args as [number, number | Function, Function?])

But that gives me this error:

Argument of type 'number | Function' is not assignable to parameter of type 'number'.

Type 'Function' is not assignable to type 'number'.

Here is the full block:

  public async chunk(max: number, offset: number, callback: (results: any[]) => void): Promise<DB>
  public async chunk(max: number, callback: (results: any[]) => void): Promise<DB>
  public async chunk(...args: (number | Function)[]): Promise<DB> {
    this.table(this.$table)
    return await super.chunk(...args as [number, number | Function, Function?])
  }

Is there anyway that I can call the super method without having to call it different times within if statements with different arguments?

TypeScript Playground

Community
  • 1
  • 1
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • What are the parameters of `this.chunk()` ? Is it also `...args`? – AskYous Mar 28 '19 at 18:20
  • `this.chunk()` and `super.chunk()` are the exact same definitions and parameters, just different bodies – Get Off My Lawn Mar 28 '19 at 18:21
  • I coded this problem in TypeScript playground and it worked for me (press run): https://www.typescriptlang.org/play/#src=class%20Parent%20%7B%0D%0A%20%20%20%20public%20myFunction(...args)%20%7B%0D%0A%20%20%20%20%20%20%20%20alert(args)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Child%20extends%20Parent%20%7B%0D%0A%20%20%20%20public%20myFunction(...args)%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20super.myFunction(...args)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20child%20%3D%20new%20Child()%3B%0D%0Achild.myFunction(1%2C%202%2C%203)%3B – AskYous Mar 28 '19 at 18:29
  • You are missing all the overload definitions – Get Off My Lawn Mar 28 '19 at 18:34
  • `super.chunk(...args as [any, any, any])` works, but that's pretty nasty. – Aaron Beall Mar 28 '19 at 18:36
  • Can you reproduce your problem in TypeScript's playground? I'm not understanding. – AskYous Mar 28 '19 at 18:38
  • @AskYous Yes I can however the link is too long to share. – Get Off My Lawn Mar 28 '19 at 18:43
  • Yeah I almost had that problem. Share the code using pastebin.com or something and I can put it into TS playground. – AskYous Mar 28 '19 at 18:44
  • @AskYous I added the link to the question – Get Off My Lawn Mar 28 '19 at 18:44
  • Thanks. I played around and noticed that typescript thinks there's only 2 functions in `super` for some reason. Not sure why: https://i.imgur.com/pBw1fSs.png – AskYous Mar 28 '19 at 18:50
  • 1
    Btw, [avoid `return await`](https://stackoverflow.com/a/43985067/1048572)! You might not even need an `async` method if you don't expect `this.table` to throw. – Bergi Mar 28 '19 at 18:52
  • There was a syntax errors in the TS playground link you shared. One of the super functions is missing its implementation. – AskYous Mar 28 '19 at 18:52
  • 1
    @AskYous That's how overloads are supposed to work. The implementation (the `...args` on in this case) is not exposed as a callable function. This is basically the problem OP is describing. It's hard to generically call overloaded functions. – Aaron Beall Mar 28 '19 at 18:55
  • Ok. Not sure I can help as I don't know about overloads in TS. – AskYous Mar 28 '19 at 18:57
  • This works `[number, number, ((results: any[]) => void)]` however, it isn't correct. – Get Off My Lawn Mar 28 '19 at 19:03
  • I guess the type should be `[number, number, Function] | [number, Function]` (or with the full type spelled out instead of `Function`) – Bergi Mar 28 '19 at 19:36
  • I just tried that too. That also errors out for me. – Get Off My Lawn Mar 28 '19 at 19:39

0 Answers0