2

colleagues!

Help me to solve the following problem.

There are several interfaces with predefined values. For example:

  interface ButtonTheme {
    readonly Black: 'BLACK';
    readonly White: 'WHITE';
    readonly WhiteOutlined: 'WHITE-OUTLINED';
    readonly Yellow: 'YELLOW';
  }

  interface ButtonType {
    readonly Checkout: 'CHECKOUT';
    readonly Pay: 'PAY';
    readonly Simple: 'SIMPLE';
  }

  interface ButtonWidth {
    readonly Auto: 'AUTO';
    readonly Max: 'MAX';
    readonly MaxImportant: 'MAX_IMPORTANT';
  }

I need a wrapper that will create a new type consisting of interface property values. To make it work:

  type Theme = Values<ButtonTheme>; 
  // 'BLACK' | 'WHITE' | 'WHITE-OUTLINED' | 'YELLOW'

  type Type = Values<ButtonType>; 
  // 'CHECKOUT' | 'PAY' | 'SIMPLE'

  type Width = Values<ButtonWidth>; 
  // 'AUTO' | 'MAX' | 'MAX_IMPORTANT'

Help to create a type Values, please

Previously I solved this problem via enum, but in the current circumstances it cannot be used.

I found one solution in the documentation, but it refers only to keys, but not to values.

1 Answers1

1

As always, there is a fairly simple solution to a complex question:

type Values<T> = T[keyof T];