Skip to content

Has no compatible call signatures error #16032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Gozala opened this issue May 23, 2017 · 2 comments
Closed

Has no compatible call signatures error #16032

Gozala opened this issue May 23, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Gozala
Copy link

Gozala commented May 23, 2017

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.

@gcanti
Copy link

gcanti commented May 23, 2017

AFAIK is a known limitation, see #7294 (comment)

The workaround I'm using in fp-ts is defining the same type parameters for both OK and Err

interface IResult<x, a> {
  map <b> (f: (input: a) => b): Result<x, b>
}

class Ok<x, a> implements IResult<x, a> {
  readonly isOk = true
  constructor(public readonly value: a) { }
  map<b>(f: (value: a) => b): Result<x, b> {
    return new Ok<x, b>(f(this.value))
  }
}

class Err<x, a> implements IResult<x, a> {
  readonly isOk = false
  constructor(public readonly error: x) { }
  map<b>(f: (value: a) => b): Result<x, b> {
    return this as any
  }
}

type Result <x, a> =
  | Ok<x, a>
  | Err<x, a>

const map = <x, a, b>(f: (input: a) => b, result: Result<x, a>): Result<x, b> =>
  result.map(f)

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 23, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 7, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Jun 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants