1

Let says I have a type as follows:

export enum configType {
  value = 'blah bala',
  value2 = 'blah bala',
  value3 = 'blah bala',
  value2 = 'blah bala',

}

Now I want to create a new type with above enum as follows:

export type analog = {
    [v + '_key' in configType]?: boolean;
}

Would this be possible?

1 Answer 1

1

You can do this with literal types the type utilities Record<Keys, Type> and Partial<Type>.

TS Playground

enum ConfigType {
  Value = 'Value',
  Value2 = 'Value2',
  Value3 = 'Value3',
  Value4 = 'Value4',
}

type Analog = Partial<Record<`${ConfigType}_key`, boolean>>;

/*

type Analog = {
  Value_key?: boolean;
  Value2_key?: boolean;
  Value3_key?: boolean;
  Value4_key?: boolean;
}

*/
Sign up to request clarification or add additional context in comments.

2 Comments

Can we also change the key-value type along with this? because currently key value is converted to any, but I need it as string.
@FRSTAR If you have a new question, please ask a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.