I encountered a TypeScript error when using reduce to create an object where the keys and values are the same. Here’s my code:
const weather = {
sunny: '맑음',
partlyCloudy: '구름 조금',
mostlyCloudy: '구름 많음',
cloudy: '흐림',
rain: '비',
snow: '눈',
rainOrSnow: '비 또는 눈',
snowOrRain: '눈 또는 비',
thunderstorm: '천둥번개',
fog: '안개',
yellowDust: '황사'
};
type WeatherKeys = keyof typeof weather;
type WeatherKeyMap = { [K in WeatherKeys]: K };
const foodKeys = (Object.keys(weather) as WeatherKeys[]).reduce((acc, key) => {
acc[key] = key;
return acc;
}, {} as WeatherKeyMap);
I’m receiving the following error on acc[key] = key:
Type 'string' is not assignable to type 'never'.
How can I resolve this error?