Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Last active June 8, 2022 01:54
Show Gist options
  • Save tonkotsuboy/6bc646d5301d5eeb15a61bec78fc9036 to your computer and use it in GitHub Desktop.
Save tonkotsuboy/6bc646d5301d5eeb15a61bec78fc9036 to your computer and use it in GitHub Desktop.
Objectのvalueからなるunion型のエイリアス
// Objectのvalueからなるunion型のエイリアス
type ObjectValueType<T> = T[keyof T];
// 使い方
const MyFood = {
Food1: "ラーメン",
Food2: "カツ丼",
Food3: "ビャンビャン麺",
} as const;
// OK
const foo: ObjectValueType<typeof MyFood> = "ラーメン";
// 「生姜焼き」はないからNG
const bar: ObjectValueType<typeof MyFood> = "生姜焼き";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment