3

Is there any way to describe this interface in typescript:

abstract class Entity { /*...*/ }
interface List<A extends Entity> { /*...*/ }

// I want the below interface
interface Collections {
  [name: string]: List<A extends Entity>;
}

The Collections object contains different List objects of classes that extend the Entity class. So the following code doesn't solve my problem:

interface Collections<A extends Entity> {
  [name: string]: List<A>;
}

The above interface serves only for one single class A, I want it to serve multiple classes extend from Entity class.

Thank you!

1 Answer 1

1

If I caught the idea, this might be the solution?

abstract class Entity { /*...*/ }
class Book extends Entity { title = 'default'; }
class Spoon extends Entity { price = 5; }

interface List<A> { /*...*/ }
interface EntityList extends List<Entity>{}

interface Collections {
  [name: string]: EntityList
}

const t: Collections = {
  'test': [new Book(), new Spoon()]
}

const x = t['test'];

If you don't wont to close type you might do this:

interface Collections<T> {
  [name: string]: List<T>
}

const t: Collections<Entity> = {
  'test': [new Book(), new Spoon()]
}

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.