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

NewableFunction not working as intended #44337

Closed
rajmondx opened this issue May 30, 2021 · 1 comment
Closed

NewableFunction not working as intended #44337

rajmondx opened this issue May 30, 2021 · 1 comment

Comments

@rajmondx
Copy link

rajmondx commented May 30, 2021

There is little info about NewableFunction except for the meeting notes in #27102 and the pr #27028.

As far as I understand the comments there, a NewableFunction should be "callable" with new Foo(...), right? Or do we have to stick with apply(...)/call(...)?

Example:

class Foo {}

function doSomething(clazz: NewableFunction) {
  new clazz();
  // This expression is not constructable.
  //   Type 'NewableFunction' has no construct signatures
}



doSomething(Foo); // passes but would fail with "normal functions" aka. CallableFunction

Question about workaround:

If NewableFunction works as desired should we stick with new Function.prototype.bind.call(clazz, undefined); or should we define our own "ConstructableFunction" (a lot of people suggest this and even angular did it):

eg.

export interface ConstructableFunction extends NewableFunction {
  new(...args: any[]): any
}
@rajmondx
Copy link
Author

rajmondx commented May 30, 2021

Ended up using something like this:

interface ConstructableFunctionTyped<T> extends NewableFunction {
  new(...args: any[]): T;
}

export type ConstructableFunction = ConstructableFunctionTyped<any>;
export type Class = ConstructableFunctionTyped<Object>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant