Description
TypeScript Version: 2.3 / nightly (2.2.0-dev.201xxxxx)
I get has no compatible call signature
error when invoking a method defined by interface that every variant of type union implements.
Here is an example code also included inline below for convenience. I am afraid it's little verbose but I could not think of ways to reduce it further:
interface IResult<x, a> {
map <b> (f:(input:a) => b):Result<x, b>
}
class Ok<a> implements IResult<any, a> {
isOk: true = true
constructor(public value: a) {
}
map<b>(f:(value: a) => b): Ok<b> {
return new Ok(f(this.value))
}
}
class Err<x> implements IResult<x, any> {
isOk: false = false
constructor(public error: x) {
}
map<b>(f:(value: any) => b):Err<x> {
return this
}
}
type Result <x, a> =
| Ok<a>
| Err<x>
export const map = <x, a, b>(f: (input: a) => b, result: Result<x, a>): Result<x, b> =>
result.map(f)
Expected behavior:
I would expect this code to type check fine given that both Ok
and Err
implement IResult
interface that defines map method map <b> (f:(input:a) => b):Result<x, b>
.
Actual behavior:
tsc reports following error:
Cannot invoke an expression whose type lacks a call signature. Type
(<b>(f: (value: a) => b) => Ok<b>) | (<b>(f: (value: any) => b) => Err<x>)
has no compatible call signatures.(parameter)
result: Ok<a> | Err<x>
** Workaround: **
It is possible to workaround this issue by down-casting passed result
to IResult<x, a>
:
(<IResult<x, a>>result).map(f)
But that means every user of the Result
library will have to acknowledged this workaround and use it in their code if they happen to do something other that what's provided by a library.