jon
jon

Reputation: 1818

how allow tuple generic to union generic?

Hi how i can allow a class with tuple as generic to be pass to a function with a class with union generic ? is it possible , is there a suggestion you can give me ? thanks

Here the code , i want pass entityA to function useEntity(entityA ). I want the function accept any Entities with components 1[]

type Component = number;
type EntityWith<C extends Component> = Entity<C[]>

abstract class Entity<C extends Component[]> {
  components!: () => C[number][]
  constructor(components: C) {
  }
}

// pass any entity with min component type 1
function useEntity(entity: Entity<1[]>) {}
//                   ^?

declare const entityA: Entity<[1, 3]>
useEntity(entityA); // how allow this because it have the min requirement [1]
//           ^?

// specialised class will look like this .
class EntityA extends Entity<[1,3]> {
  constructor() {
    super([1,3])
  }
}

playground

Upvotes: 1

Views: 41

Answers (0)

Related Questions