Last active
June 8, 2022 01:54
-
-
Save tonkotsuboy/6bc646d5301d5eeb15a61bec78fc9036 to your computer and use it in GitHub Desktop.
Objectのvalueからなるunion型のエイリアス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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