Skip to content

inconsistency in type inference on union of function types  #9104

Closed
@Artazor

Description

@Artazor

TypeScript Version:

1.8.10 (seems, that other are affected as well)

Code

//TYPE DOMAIN
type T1 = (arg: string|number) => string;
type T2 = (arg: string) => string;

declare var fn: T1 | T2;


var res = fn('x'); // error TS2349: 
                   // Cannot invoke an expression 
                   // whose type lacks a call signature.

// VALUE DOMAIN
const f1 = (arg: string|number) => 'x';
const f2 = (arg: string) => 'y';

declare var a: boolean;
const fn1 =  a ? f1 : f2
const fn2 = !a ? f2 : f1

var res1 = fn1('x'); // Success, fn1 inferred as (arg: string|number) => string
var res2 = fn2('x'); // Success, fn2 inferred as (arg: string) => string

Expected behavior:

  • Code in type domain at least should compile.
  • In both domains the correct type for the function should be (arg: string) => string and should not be dependent on the order of union members / conditional branches.

Actual behavior:

  • Code in type domain fails to compile with error TS2349: Cannot invoke an expression whose type lacks a call signature.
  • In value domain the type is dependent on the order of conditional branches

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions