The following code doesn’t work, as the converter function by Typescript should expect a parameter typed Filters
. How can we make this work, so that the argument is based on the type of the key
of the object?
type Filter1 = {
type: 'filter1',
value1: number
}
type Filter2 = {
type: 'filter2',
value2: number;
}
type Filters = Filter1 | Filter2;
const converter: Record<Filters['type'], (filter: Filters) => string> => {
filter1: (filter: Filter1) => {
return filter1.value1.toString();
},
filter2: (filter: Filter2) => {
return filter2.value2.toString();
},
}