question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConstructorOf<T> wrapper to declare type of constructor of type parameter

See original GitHub issue

so 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:open
  • Created 5 years ago
  • Reactions:7
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
alecgibsoncommented, Mar 10, 2021

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”
3reactions
championswimmercommented, Sep 20, 2018

Is this a suggestion to add that type to the global scope?

Yes it is! Would be useful lot of places 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found