Is there a type for "Class" in Typescript? And does "any" include it?
Angular internally declare Type
as:
export interface Type<T> extends Function { new (...args: any[]): T; }
With TypeScript3 it should be possible to add types for arguments without function overloading:
export interface TypeWithArgs<T, A extends any[]> extends Function { new(...args: A): T; }
Example:
class A {}
function create(ctor: Type<A>): A {
return new ctor();
}
let a = create(A);
The simplest solution would be let variable: typeof Class
.
Here an example:
class A {
public static attribute = "ABC";
}
function f(Param: typeof A) {
Param.attribute;
new Param();
}
f(A);
The equivalent for what you're asking in typescript is the type { new(): Class }
, for example:
class A {}
function create(ctor: { new(): A }): A {
return new ctor();
}
let a = create(A); // a is instanceof A
(code in playground)
The code above will allow only classes whose constructor has no argument. If you want any class, use new (...args: any[]) => Class