1

I have an abstract class called Action:

abstract class Action { ... }

And I have several classes that extend Action:

class Vote extends Action { ... }
class Upload extends Action { ... }
...

How can I declare an array of classes that extends Action (but shouldn't accept Action itself?

const actions: (typeof Action)[] = [
  Vote,    // OK (passes)
  Upload,  // OK (passes)
  File,    // OK (fails)
  Action   // BAD (passes)
]

1 Answer 1

5

Use a constructor signature that returns the abstract class instead of the abstract class. The abstract class does not have a constructor in the type system so it won't be valid but any derived class inheriting the abstract class will be valid.

abstract class Action { ... }

class Vote extends Action { ... }
class Upload extends Action { ... }

const actions: (new () => Action)[] = [
  Vote,    // OK (passes)
  Upload,  // OK (passes)
  File,    // OK (fails)
  Action   // Ok now (fails)
]
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.