I've been through many parts of typescript docs like mapped types, generics,... but honestly couldn't find a solution.
Problem:
Assuming a have an object like this with corresponding type:
const filters: {
age: { value: number, isActive: boolean },
city: { value: string, isActive: boolean },
} = {
age: { value: 25, isActive: true },
city: { value: "tokyo", isActive: false },
};
Later in my code, I may transfer the data into this different shape:
interface desiredShape {
filter: // either age or city
value: // if filter == age, should be of type number, if filter == city, should be of type string
}
So the following assignment is correct:
const obj2 : desiredShape = {filter: "city", value: "Vienna"}
How can I achieve that type?