Skip to content

strict function type does not handle spread arguments #20645

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

Closed
beckend opened this issue Dec 12, 2017 · 5 comments
Closed

strict function type does not handle spread arguments #20645

beckend opened this issue Dec 12, 2017 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@beckend
Copy link

beckend commented Dec 12, 2017

TypeScript Version: 2.7.0-dev.20171212 and 2.6.2

Code

class TargetComponent {

  public one: any;
  public two: any;

  constructor(one: any, two: any) {
    this.one = one;
    this.two = two;
  }

}

class ComponentExt extends TargetComponent {

  constructor(...args: any[]) {
    // Expected 2 arguments, but got 0 or more.
    super(...args);
  }

}

Expected behavior:
No errors.

Actual behavior:
at Super => Expected 2 arguments, but got 0 or more.

@ghost
Copy link

ghost commented Dec 12, 2017

This isn't a --strictFunctionTypes error.
It will work if the constructor were defined as one?: any, two?: any. The problem is that with an arbitrary array, we can't be sure it has isn't empty, so we can't know that both arguments are provided. See #15849.

@ghost ghost added the Question An issue which isn't directly actionable in code label Dec 12, 2017
@beckend
Copy link
Author

beckend commented Dec 12, 2017

Alright, you are saying this valid javascript is not accepted in typescript in strict mode? And also that it is a feature?

@ghost
Copy link

ghost commented Dec 12, 2017

This isn't related to the --strict flag, it shoul happen regardless of compiler options.
Since you're typing everything as any right now you can get this code to work by making the parameters optional. But if you wanted to ensure that they're not undefined, then you also need to ensure that all super calls pass in at least two arguments.
Also, in JavaScript it's not necessary to declare a constructor if all you're doing is delegating to a parent class constructor, so if constructor(...args: any[]) { super(...args); } is really all you're doing, you could just remove that and it will work.

@beckend
Copy link
Author

beckend commented Dec 12, 2017

The code was more of a demonstration, of course the point with that constructor was to make something happen, and in many cases you don't really need to care about the arguments and thus you use spread operator. The demonstration might as well be proxying two functions and it would produce the same error.

But so far I get that this is indeed intentional from typescript and this was actually a aware decision you made...which bring this issue to it's conclusion so you can close this at your discretion.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants