0

I am trying to create a more dynamic object typing. Right now I have the following:

interface MyGeneric<T> {
  value: T
}

interface MyObject {
  [name: string]: MyGeneric<any>;
}

But I want to abstract it to look more like this:

interface MyGeneric<T> {
  value: T
}

interface MyObject {
  [name: string]<T>: MyGeneric<T>;
}

This syntax does not work, but what I want would allow each prop on the object to have its own generic type.

Any advice on what this might look like?

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
  • 1
    This reminds me of [a similar question I asked but for Swift](https://stackoverflow.com/q/61750544/8289918). I don't think this is possible because the `MyGeneric`s in `MyObject` would have completely different types unless they have the same type parameter (e.g. `any`). For example, what type would `MyObject.foo` be? TypeScript does not have a type that would be able to describe this. – Lauren Yim Aug 30 '20 at 02:13
  • How would you add the type for the generic in the second example? You may want to add details about your use case so we can try to help find a solution using a related type like mapped types. – evelynhathaway Jun 05 '21 at 02:01

0 Answers0