#Create a type with keys based on array of types

2 messages · Page 1 of 1 (latest)

humble gate
#

I'm wondering if the following is possible on the type level in TypeScript. Let me demonstrate:
Say, I have the following base class

abstract class Base<T extends string> {
  private readonly __name: T
  private static readonly __name: string
}

And a type that can extract the T

type GetName<PC> = PC extends typeof Base<infer T> ? T : never

Users can extend the Base and add properties (using a mixin, which is not too important here, but it sets the static property, so it can be read back)

class MyClassA extends FromBase('a') {
  prop: string
}

Then I want to create a new class which combines the others:

class Combined extends FromParts(MyClassA, MyClassB, ...)

And this should produce a type like:

{
  a: MyClassA,
  b: MyClassB
}

I can't figure out how to do that.. mabye it would be possible with a limited number of elements, but not as an unbounded array, what do you think?

sweet birch
#

Without you providing a playground, not sure if this is what you want.