Suppose I have a dictionary object like this:
const x = {
foo: {inner: 3},
bar: {inner: 'hi'},
};
The inner property has different types (in this case, string and number).
My goal is to transform it into a structure that appears as follows:
const y = {
foo: 3,
bar: 'hi',
};
However, I want to achieve this transformation automatically while preserving the type information. Can this be done in Typescript?
I've made some progress using lodash:
import { mapValues } from 'lodash';
const y: Y = mapValues(x, (z) => z.inner);
But this results in a union of all types in the dictionary:
const y: {
foo: string | number;
bar: string | number;
}
Instead of what I actually want:
const y: {
foo: number;
bar: string;
};