您可以显式定义将成为
BACKGROUND_IMGS
关键点和定义
Record
其中:
type YourElement = "fire" | "water";
export const BACKGROUND_IMGS: Record<YourElement, string> = {
fire: require("..."),
water: require("...")
};
为了避免重复使用密钥,另一种方法是使用
keyof
要使用objects键创建新类型并使用该键声明新类型,请执行以下操作:
const BACKGROUND_IMGS = {
fire: require(".."),
water: require("..")
};
type ImageKeys = keyof typeof BACKGROUND_IMGS;
export const BACKGROUND_IMGS_TYPED = BACKGROUND_IMGS as Record<ImageKeys,string>;