How can I implement the Foo
interface when it has a conditional type as its return type?
interface Foo {
<A, B>(a: A, b: B): A extends B ? string : number
}
const foo: Foo = (a, b) => a === b ? 'str' : 123
The compiler is generating this error:
Type '"str" | 123' is not compatible with type 'A extends B ? string : number'.
Type '"str"' is not assignable to type 'A extends B ? string : number'.
I have come across this question but I am struggling to apply it to my specific example. Additionally, I am interested in knowing the official or correct approach, rather than just workarounds.
UPDATE: I managed to resolve it similar to the solution provided in the mentioned question. Unional's answer really helped me understand the issue. This is how I solved it:
interface Foo {
<A, B>(a: A, b: B): A extends B ? string : number
<A, B>(a: A, b: B): string | number
}