🦴
配列から型を生成してオブジェクトのKeyに指定する備忘録
何?
const USER_KEYS = ["name", "mail", "address", "tel"];
みたいなのから
type UserKeys = "name" | "mail" | "address" | "tel";
を生成してProfileみたいなオブジェクトのKeyに指定したいんです。毎回忘れるのでメモ。
どないするん
as constしてtypeof
const USER_KEYS = ["name", "mail", "address", "tel"] as const;
type UserKeys = typeof USER_KEYS[number];
なんでできるんかはこれが詳しそう
key in
const ksyunnnn: { [key in UserKeys]: string } = {} // 補完走る
Discussion