Typescript - type to represent any class?

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 .)

+7
source share
1 answer

You should avoid using the Object type in typewriting; it's better to use any as the docs say :

You can expect Object to play a similar role as in other languages. But variables of type Object only allow you to assign them any value - you cannot call arbitrary methods for them, even those that really exist

But if you want to represent classes, you need to have the following form:

 { new (): CLASS_TYPE } 

Or in your case:

 function shuffle(classes: Array<{ new (): any }>): Array<{ new (): any }> { return []; } class A { } class B extends A { } class C extends B { } class D extends B { } shuffle([A, B, C, D]); 

( ): Array <{new (): any}> {return []; } class A {} class B extends A {} class C extends B {} class D extends B {} shuffle ([A, B, C, D]); rel = noreferrer> code on the playground )

If all your classes are based on a superclass (as your example shows), you can simply do:

 function shuffle(classes: Array<{ new (): A }>): Array<{ new (): A }> { return []; } 

edit

Just saw what you want

sort classes by level expands from object

To answer this:

 function shuffle(classes: Array<{ new (): any }>): Array<{ new (): any }> { return classes.sort((a, b) => getInheritanceLevel(a) - getInheritanceLevel(b)); } function getInheritanceLevel(cls: { new (): any }): number { let level = 0; while (Object.getPrototypeOf(cls.prototype) !== Object.prototype) { level++; cls = Object.getPrototypeOf(cls.prototype).constructor; } return level; } shuffle([D, A, C, B]); // returns [A, B, D, C] 

( ): Array <{new (): any}> {return classes.sort ((a, b) => getInheritanceLevel (a)% 20- getInheritanceLevel (b)); } function getInheritanceLevel (cls: {new (): any}): number {let level = 0; while (Object.getPrototypeOf (cls.prototype)! == Object.prototype) {level ++; cls = Object.getPrototypeOf (cls.prototype) .constructor; } return level; } class A {} class B extends A {} class C extends B {} class D extends B {} shuffle ([D, A, C, B]); // returns [A, B, D, C] rel = noreferrer> code on the playground )

+8
source

All Articles