0

My question is similar with Get all value types of a double-nested object in TypeScript

But the difference is, I want to get union types from the value of a specific property.

const tabsEnum = {
  IDCardReview: {
    label: 'ID Card',
    key: '1',
  },
  PrescriptionReview: {
    label: 'Prescription Review',
    key: '2',
  }
} as const;

I want to get a union types like type Key = '1' | '2'. Which means use the value of the key property in the each object.

Lin Du
  • 88,126
  • 95
  • 281
  • 483

1 Answers1

0
const tabsEnum = {
  IDCardReview: {
    label: 'ID Card',
    key: '1',
  },
  PrescriptionReview: {
    label: 'Prescription Review',
    key: '2',
  }
} as const;

type Tabs = typeof tabsEnum;
type ValueOf<T> = T[keyof T];
type ValueTabs = ValueOf<Tabs>;
type K = ValueTabs['key'];

TypeScript Playground

Lin Du
  • 88,126
  • 95
  • 281
  • 483