Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling super from an overloaded constructor #21906

Closed
westy92 opened this issue Feb 12, 2018 · 8 comments
Closed

Calling super from an overloaded constructor #21906

westy92 opened this issue Feb 12, 2018 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@westy92
Copy link

westy92 commented Feb 12, 2018

TypeScript Version: 2.8.0-dev.20180211
(Tested in the above version, but found in 2.7.1)

Search Terms:
overload constructor super
super expected arguments got 0 or more.

Code:

class MyDate extends Date {

  public constructor();
  public constructor(vd: VarDate);
  public constructor(value: Date);
  public constructor(value: number);
  public constructor(value: string);
  public constructor(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number);
  public constructor(...args: any[]) {
    super(...args);
    // sublcass-specific code
  }

}

Expected behavior:
Code compiles without error.

Actual behavior:
Error: Expected 0-7 arguments, but got 0 or more.

Playground Link:
FYI it seems a bit extreme that it just puts the source in the URL, but here it is:
https://www.typescriptlang.org/play/#src=class%20MyDate%20extends%20Date%20%7B%0D%0A%0D%0A%20%20public%20constructor()%3B%0D%0A%20%20public%20constructor(vd%3A%20VarDate)%3B%0D%0A%20%20public%20constructor(value%3A%20Date)%3B%0D%0A%20%20public%20constructor(value%3A%20number)%3B%0D%0A%20%20public%20constructor(value%3A%20string)%3B%0D%0A%20%20public%20constructor(year%3A%20number%2C%20month%3A%20number%2C%20date%3F%3A%20number%2C%20hours%3F%3A%20number%2C%20minutes%3F%3A%20number%2C%20seconds%3F%3A%20number%2C%20ms%3F%3A%20number)%3B%0D%0A%20%20public%20constructor(...args%3A%20any%5B%5D)%20%7B%0D%0A%20%20%20%20super(...args)%3B%0D%0A%20%20%20%20%2F%2F%20sublcass-specific%20code%0D%0A%20%20%7D%0D%0A%0D%0A%7D

Related Issues:
#1790
#11679

@RyanCavanaugh
Copy link
Member

@sandersn any ideas on what to do here? My initial reaction was (super as any)(...args) but that doesn't compile for multiple reasons. I was also surprised super(...(args as []); didn't work

@westy92
Copy link
Author

westy92 commented Feb 12, 2018

I tried this:

// super(...args);
super();
Date.apply(this, args);

It compiles, but some unit tests are now failing. I'm not sure if the failures are related or not.

@westy92
Copy link
Author

westy92 commented Feb 12, 2018

I also tried:

super(...(args.slice(0, 7)));

No luck, but I didn't like that solution anyways.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 12, 2018

assuming #4130 is fixed, you can cast to a tuple.

Until then, //@ts-ignore is your best bet here.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 12, 2018
@sandersn
Copy link
Member

@westy92 I have most of #4130 fixed at the branch spread-tuples if you want to try it to see if it fixes your problem. I haven't had time to test it with generics, but you don't have that here.

@westy92
Copy link
Author

westy92 commented Feb 13, 2018

@sandersn I got the same error using your branch. Do I need to tweak my above code?

@sandersn
Copy link
Member

Yes, you'll need to cast to a tuple of some length that is greater than the smallest date length, but I think even [number] would suffice. ([] isn't a valid tuple type in typescript even though it's understood and would be correct here).

You'll also need to build Typescript yourself and compile from $typescript/built/local/tsc.js. I didn't update the last-known-good bits yet, so an npm install won't suffice.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants