ConstructorOf<T> wrapper to declare type of constructor of type parameter
See original GitHub issueso if I have
class MyClass {
}
Then the type of MyClass.prototype
is MyClass
And the type of MyClass
is typeof MyClass
If I want these two functions
function takesPrototype(p: MyClass) { /* ... */ }
function takesClass(p: typeof MyClass) { /* ... */ }
I can call them as
takesPrototype(MyClass.prototype)
takesClass(MyClass)
But what if MyClass
was defined as a type parameter.
For example if I want to have a function that of this type -
const mod = getModule(ModuleClass)
// mod should be of type ModuleClass
I would like to define this as such
function getModule<M extends Module>(moduleClass: typeof M): M {
// something like a getInstance() pattern
}
The problem with this is typeof M
will complain that I am trying to use M as a value while it is a type.
So I suggest a type wrapper of this type
type ConstructorOf<C> = { new( ...args: any[] ): C }
Now I can define my function as such
function getModule<M extends Module>(moduleClass: ConstructorOf<M>): M {
// something like a getInstance() pattern
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:6 (1 by maintainers)
Top Results From Across the Web
TS1092: Type parameters cannot appear on a constructor ...
(A) declare Wrapper class directly - THIS DOESN'T WORK // The errors below are all 'TS1092: Type parameters cannot appear on a constructor ......
Read more >Defining a constructor for a typeclass that takes a method with ...
The problem with this is that function changeType takes a type parameter. Despite some googling I have not figured out how to write...
Read more >std::make_unique - cppreference.com
Constructs an object of type T and wraps it in a std::unique_ptr. 1) Constructs a non-array type T . The arguments args are...
Read more >Entity types with constructors - EF Core - Microsoft Learn
It's possible to define a constructor with parameters and have EF Core call this constructor when creating an instance of the entity.
Read more >Constructor in C++ and Types of Constructors - Great Learning
The syntax for defining constructor inside the class body is as follows: class CLASSNAME { ……… public : CLASSNAME([parameter_list]) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This would definitely be a nice global generic to have access to!
My one comment is that it should probably be called
Constructor<T>
, since<T>
is sort of read as “of ‘T’” already.For example:
Array<number>
: “array of numbers”Map<string, boolean>
: “map of string to boolean”Partial<MyInterface>
: “partial of my interface”Yes it is! Would be useful lot of places 😃