edkeveked
edkeveked

Reputation: 18381

modify interface key using generic value

I am trying to generate a type using a value provided by a generic. Here is my attempt that doesn't compile

type modifyKey<T extends 'a' | 'b'> = {
    [id: `${T}changed`]: T | string;
    another: number
}

Upvotes: 0

Views: 38

Answers (1)

type modifyKey<T extends 'a' | 'b'> = {
  [Prop in T as `${T}changed`]: T | string;
} & {
   another: number
}

// type Result = { achanged: string;   bchanged: string; another: number; }
type Result = modifyKey<'a' | 'b'> 

Here you can find docs for property renaming

P.S. According to convention, type name should be capitalized

Upvotes: 2

Related Questions