In Typescript we have way to access types from array as below.
const list = [item,item2] as const;
type typeOfArray = typeof list[number];
const something: typeOfArray = ; // item | item2
I have attempted to get that with class but getting "Property 'prototype' is missing in type error"
If I declare classes as type it works fine but can't access through typeof. Is this shortcoming of Typescript? If you have solution, will appreciate your help.
please use below code as sample setup:
class A {
methodA() {
}
}
class B {
methodB() {
}
}
class C {
methodC() {
}
}
const classList = [A,B,C] as const;
This works just fine:
type classSelection = A | B | C;
let o: classSelection;
o = new A();
o. // o.methodA;
This is giving above error:
type classSelection = typeof classList[number];
o = new A(); //Type 'A' is not assignable to type 'typeof A | typeof B | typeof C'.
Property 'prototype' is missing in type 'A' but required in type 'typeof C'
ClassSelectionTypeis a union of constructor types; if you want to turn that into a union of instance types you can use theInstanceTypeutility type like this; if that meets your needs I can write up an answer; let me know.typeoffor them changes - with this operator, you get the static side, meaning you can only assign a constructor toowhile what you are trying to assign is an instance, hence the error