Open championswimmer opened 6 years ago
Seems like you solved your problem 😉
Is this a suggestion to add that type to the global scope?
Is this a suggestion to add that type to the global scope?
Yes it is! Would be useful lot of places :)
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"Any news on Constructor<T>
being added natively in TypeScript ?
Doesn't seem like it would be difficult nor is there people against it.
I ran into this issue as well, I thought something like that should exist natively already, until I found this issue. My use case is:
export type Constructor<C> = { new (...args: any[]): C; };
export function newArray<T>(constructor: Constructor<T>, n: number): T[] {
return doTimes(()=>new constructor, n);
}
To simply generate an array of any kind of class:
var quats = newArray(pc.Quat, 10);
// quats is now: (10) [Quat, Quat, Quat, Quat, Quat, Quat, Quat, Quat, Quat, Quat]
Maybe I'm missing something and there is already a shorter way to create an array of n
objects?
It would be cool to have this, it's really annoying to have to define it ourselves. It would probably take less than an hour to implement on TypeScript's end.
so if I have
Then the type of
MyClass.prototype
isMyClass
And the type ofMyClass
istypeof MyClass
If I want these two functions
I can call them as
But what if
MyClass
was defined as a type parameter. For example if I want to have a function that of this type -I would like to define this as such
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
Now I can define my function as such