Skip to content

Has no compatible call signatures error #16032

Closed

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.

Activity

  1. gcanti commented on May 23, 2017

    @gcanti

    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)
  2. added
    Duplicate
    on May 23, 2017
  3. mhegazy commented on Jun 7, 2017

    @mhegazy
    Contributor

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

  4. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions