이 연습은 as const 어노테이션을 포함하는 programModeEnumMap 으로 시작합니다.

export const programModeEnumMap = {
  GROUP: "group",
  ANNOUNCEMENT: "announcement",
  ONE_ON_ONE: "1on1",
  SELF_DIRECTED: "selfDirected",
  PLANNED_ONE_ON_ONE: "planned1on1",
  PLANNED_SELF_DIRECTED: "plannedSelfDirected",
} as const

이번에는 programModeEnumMap 으로 인덱싱하고 거기에서 유니온 타입을 가져오고 싶습니다. 보다 구체적으로, 테스트에서 볼 수 있듯이 "1on1" | "selfDirected" | "planned1on1" | "plannedSelfDirected" 유니온이 되기를 원합니다.

type tests = [
  Expect<
    Equal<
      IndividualProgram,
      "1on1" | "selfDirected" | "planned1on1" | "plannedSelfDirected"
    >
  >
]

Challenge

IndividualProgram 타입부터 시작하세요.

export type IndividualProgram = unknown

여러분의 과제는 unknown 값을 위의 유니온 값으로 대체하는 것입니다.

생각보다 간단하지만 새로운 것을 도입하는 것이죠!

나의 답

import { Equal, Expect } from '../helpers/type-utils';

export const programModeEnumMap = {
  GROUP: 'group',
  ANNOUNCEMENT: 'announcement',
  ONE_ON_ONE: '1on1',
  SELF_DIRECTED: 'selfDirected',
  PLANNED_ONE_ON_ONE: 'planned1on1',
  PLANNED_SELF_DIRECTED: 'plannedSelfDirected',
} as const;

type IndividualProgramMap = Pick<
  typeof programModeEnumMap,
  | 'ONE_ON_ONE'
  | 'SELF_DIRECTED'
  | 'PLANNED_ONE_ON_ONE'
  | 'PLANNED_SELF_DIRECTED'
>;
export type IndividualProgram =
  IndividualProgramMap[keyof IndividualProgramMap];

type tests = [
  Expect<
    Equal<
      IndividualProgram,
      '1on1' | 'selfDirected' | 'planned1on1' | 'plannedSelfDirected'
    >
  >
];

Pick으로 일일히 찾았는데 이렇게 해도 될까?

Indexed Access를 사용하여 유니온에서 특정 멤버 추출하기

이 문제를 해결할 수 있는 몇 가지 방법이 있습니다.

해결책1

실제로 indexed access에 유니온을 전달하여 원하는 특정 멤버를 가져올 수 있습니다.