Can someone help me troubleshoot this issue with config[curr][targetEnv]
???
interface EnvType {
dev: string;
staging: string;
production: string;
}
type Config = {
[key: string]: number | string | EnvType;
};
const config: Config = {
networkTimeout: 10000,
apiHost: {
dev: 'http://localhost:8080',
staging: 'https://staging.example.com',
production: 'https://example.com',
},
};
const targetEnv = process.env.TARGET_ENV || 'dev';
const mappedConfig = Object.keys(config).reduce((acc, curr) => {
const currValue = config[curr];
// check if value is not an object, return the value
if (typeof currValue !== 'object') {
return { ...acc, [curr]: currValue };
}
// if value is an object, retrieve value based on environment
if (typeof currValue === 'object') {
return { ...acc, [curr]: config[curr][targetEnv] }; // encountering an error here
}
// if none of the above conditions are met, do nothing
return { ...acc };
}, {});
export default mappedConfig;