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
I want to suggest a new pattern Newable<T> which I got from here.
I had the problem that I wanted to pass the class itself as the type of a parameter to a function (so I can use derived classes on this parameter), but the type of the parameter is an abstract class, so TS told me it isn't newable / it has no construct signatures (hard to explain, please look at the example).
I searched through tsdef but couldn't find something that would fix my issue (or I'm just blind, so in that case please tell me), so I searched around and found the above linked stackoverflow answer.
It would be cool if you could tell me if this can already be achieved with tsdef. If not, I'd be happy to submit a PR.
My situation:
abstractclassA{}classBextendsA{constructor(){super();}}functiontest(MyClass: typeofA)// (similar error when removing typeof){letfoo=newMyClass();// ^^^^^^^^^^^^^// This expression is not constructable.// Type 'A' has no construct signatures. ts(2351)}test(B);
Example with Newable<T>:
exporttypeNewable<T>={new(...args: any[]): T;};abstractclassA{}classBextendsA{constructor(){super();}}functiontest(MyClass: Newable<A>){letfoo=newMyClass();// no error}test(B);// works fine
The text was updated successfully, but these errors were encountered:
Sv443
changed the title
Newable<T> to ensure a class that is non-abstract and inherits from TNewable<T> to ensure parameter is a class that is non-abstract and inherits from TApr 1, 2021
Sv443
changed the title
Newable<T> to ensure parameter is a class that is non-abstract and inherits from T
Newable<T> to ensure parameter is a class that is non-abstract and inherits from T
Apr 1, 2021
I want to suggest a new pattern
Newable<T>
which I got from here.I had the problem that I wanted to pass the class itself as the type of a parameter to a function (so I can use derived classes on this parameter), but the type of the parameter is an abstract class, so TS told me it isn't newable / it has no construct signatures (hard to explain, please look at the example).
I searched through tsdef but couldn't find something that would fix my issue (or I'm just blind, so in that case please tell me), so I searched around and found the above linked stackoverflow answer.
It would be cool if you could tell me if this can already be achieved with tsdef. If not, I'd be happy to submit a PR.
My situation:
Example with
Newable<T>
:The text was updated successfully, but these errors were encountered: