microsoft / microsoft/TypeScript
ConstructorOf<T> wrapper to declare type of constructor of type parameter
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the issue's proposed ConstructorOf wrapper and the existing distinction between type parameters and constructor values in TypeScript. Determine the type-system and compiler entry points involved, then define the supported generic-constructor behavior and validate it with focused type-checking tests covering getModule and constructor arguments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100