Are there alternative methods for iterating through object keys, transforming values, and ensuring the resulting type maintains the same keys as the input?
const env = {
KEY_1: "VALUE_1",
KEY_2: "ANOTHER_VALUE_2"
};
function mapValuesToLength<TEnv extends Record<string, string>>(
env: TEnv
): Record<keyof TEnv, number> {
const result: Partial<Record<keyof TEnv, number>> = {};
for (const envKey in env) {
result[envKey] = env[envKey].length;
}
return result as Record<keyof TEnv, number>;
}
// expecting result = {KEY_1: 7, KEY_2: 15}
const result = mapValuesToLength(env);
// expecting type resultType = { KEY_1: number; KEY_2: number; }
type resultType = typeof result