i'm curious that usage of enum with keyof keyword.
here is the sample code without enum.
const map = new Map<string, string>();
interface ShopStoreProps {
google: string;
apple: string,
galaxy: string
}
const setValue = <T extends keyof ShopStoreProps>(shopProps: T, value: string) => {
return map.set(shopProps, value);
}
setValue('google', 'test1'); // ok
setValue('appleee', 'test2'); // error
but I want to use only enum not the upper interface like this.
of course I know that the below setValue function is wrong grammatically.
thank you for reading this question. :)
const map = new Map<string, string>();
enum ShopStoreEnum {
GOOGLE = 'google',
APPLE = 'apple',
GALAXY = 'galaxy',
}
const setValue = <T extends keyof ShopStoreEnum>(shopProps: T, value: string) => {
return map.set(shopProps.name, value);
}
setValue(ShopStoreEnum.GOOGLE, 'test1'); // to be
setValuehave to be generic on typeshopProps?