Skip to content

Union of function signatures cannot be invoked because it lacks a call signature #4612

Closed
@Arnavion

Description

@Arnavion

Reduced test case:

var foo: ((arg: string) => void) | ((arg: any) => void);
foo(null);
  1. With 1.5.3, this compiles fine. With 1.6-beta, this gives error TS2349: Cannot invoke an expression whose type lacks a call signature. null is assignable to both string and any so it should compile.
  2. Maybe related - getting quickinfo for foo on the second line does throw an error TypeError: Cannot read property 'flags' of undefined in playground even though it compiles fine with 1.5.3. I didn't try quickinfo on 1.6
  3. If foo was ((arg: string) => void) | ((arg: number) => void); (number instead of any in the second signature) then both 1.5.3 and 1.6 give that error, even though again null is assignable to both.

For motivation, the original code is https://github.com/Arnavion/libjass/blob/6606413/src/utility/promise.ts#L363 (the error is on handler). It's an implementation of the ES6 promise spec, specifically http://www.ecma-international.org/ecma-262/6.0/#sec-promisereactionjob - handler would be either the fulfilled callback or rejected callback and the argument passed to it would be either the resolution value or the rejection reason accordingly, hence its signature.

(I understand it would be more type-safe to have separate handlers for the two cases with one signature each from the union, but that complicates the implementation slightly.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions