I want to call a function with an object as only argument. This object can be two different interfaces which collides (1+ properties have the same key but different possible values).
In my main function, I want to call getFoo if the type value in the object if foo and getBar if the type value in the object if bar.
interface Foo {
title: string
number: 1 | 2
}
interface FooWithType extends Foo {
type: 'foo'
}
interface Bar {
title: string
number: 3 | 4
}
interface BarWithType extends Bar {
type: 'bar'
}
function getFoo(params: Foo): void
function getBar(params: Bar): void
function main({ type, ...rest }: FooWithType | BarWithType) {
return type === 'foo' ? getFoo(rest) : getBar(rest)
}
I have a TypeScript issue when I'm doing a conditional type on a destructured object where Type '{ title: string; number: 3 | 4; }' is not assignable to type 'Foo' because my rest value is still an union type when I typecheck it:
var rest: {
title: string;
number: 1 | 2;
} | {
title: string;
number: 3 | 4;
}