//Type declaration:
interface TickListFilter {
type: "tickList";
value: string;
}
interface ColorFilter {
type: "color"
value: ColorValueType
}
type Filter = TickListFilter | ColorFilter;
...
onValueChange = (filter: Filter, newValue: Filter["value"]) => {
if (filter.type === "tickList") {
// variable 'filter' has TickListFilter type (OK)
// variable 'filter.value' has string type (OK)
// variable newValue has ColorValueType | string. I know why, let's fix it!
}
...
onValueChange = <T extends Filter>(filter: T, newValue: T["value"]) => {
if (filter.type === "tickList") {
// variable 'filter' has Filter type (wrong... i need tick list)
// variable 'filter.value' has string | ColorValueType type (wrong, it should be string)
// variable newValue has ColorValueType | string.
}
How I can fix that? I know why it happens, because TS cant discriminate union by generic type. But is there any workaround to work as I described?
I want to use similar structure as switch-case (not if-else only)