0

Lets say you have an interface then classes that implement it.

interface A { /*do stuff*/ }

class B implements A { /*do stuff*/ }

class C implements A { /*do stuff*/ }

Then if you have a variable that can store the class type, not a specific instance of an object, what do you put as the type?

let x: Something = B; // or C

I know for objects you can use typeof, but for interfaces you can't. However I am able to specify variables to store implementations of interfaces so I don't see why I shouldn't be able to store classes of an interface.

1 Answer 1

1

You can use a constructor signature (similar to a function signature, but with a new in front of it):

interface A { /*do stuff*/ }

class B implements A { /*do stuff*/ }

class C implements A { /*do stuff*/ }


let x: new () => A = B;
let x2: new () => A = C;

Playground Link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.