0
votes

This is basically what I was hoping to accomplish but it is no go for launch:

type MyType<
  T extends {},
  K extends string = 'keyName'
> = T & {
  [K]: {
    value: string
  }
};

A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.ts

Is there any way to pass in a key and have it shape the resulting type or interface?

Thanks.

1

1 Answers

1
votes

You need to use [key in K].

type MyType<
  T extends {},
  K extends string = "keyName"
  > = T & {
    [key in K]: {
      value: string
    }
  };

Playground