You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extracting the instance type from the ctor value that works for abstract classes #26829
Currently, I think there is no way to express the type of constructor functions for abstract classes. On one hand that is correct because 'new' cannot be called on them. However, sometimes there is a need to tie an instance of the abstract class with the constructor function for type-checking.
Use Cases
DI helper functions in angular and other frameworks.
Examples
// Angular calls this Type<T>// https://github.com/angular/angular/blob/master/packages/core/src/type.ts#L25interfaceFactory<T>extendsFunction{new(...args: any[]): T;}abstractclassAC{abstractx: number;}classCextendsAC{x=0;}// a DI-like system, that returns instance of C for get(C) and get(AC).// The mechanism is left unspecified.declarefunctionget<T>(token: Factory<T>): T;letx1=get(C);letx2=get(AC);// error, underlying issue is that AC is not assignable to Factory<AC>
Basically, this is asking for a mechanism to go from a value C, where C is a symbol for a class (abstract or not), to the type C.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
Closing this issue in favor of #24428. In practice both AngularJS and Angular DI types are written using an second overload to :any to handle the cases where the issue described above prevents from type checking :/
Activity
mattmccutchen commentedon Sep 3, 2018
Could you just use the prototype?
This doesn't work for generic classes, but I'm not sure how your proposal could either.
rkirov commentedon Sep 18, 2018
Closing this issue in favor of #24428. In practice both AngularJS and Angular DI types are written using an second overload to
:anyto handle the cases where the issue described above prevents from type checking :/