What type should I use in typewriting to represent any class?
I am trying to write a function that takes an array of classes and returns an array in a different order.
function shuffle(classes: typeof Object[]) : typeof Object[] { return ...; } class A { } class B extends A { } class C extends B { } class D extends B { } suffle([A, B, C, D]);
An argument of type 'typeof A []' cannot be assigned to a parameter of type 'ObjectConstructor []'.
Then I tried:
shuffle([typeof A, typeof B, typeof C, typeof D]);
error TS2345: An argument of type 'string []' cannot be assigned to a parameter of type 'ObjectConstructor []'. The type 'string' cannot be assigned to the type 'ObjectConstructor'.
Which is the right way? Generics? How? This does not work:
export function <T extends typeof Object> shuffle(classes: T[]) : T[]
It's neither
export function <T extends Object> sortClassesBySpeciality(classes: typeof T[]) : typeof T[]
Also, why typeof (typeof A) is "string" and "" + typeof A is function ? Well, I realized typeof has two completely different context values for defining a type and an expression.
(The ultimate goal is to sort the classes by extends from Object .)
source share