Skip to content

Type inference for conditional callback parameters breaks down #22149

Closed
@krryan

Description

@krryan

This is based on #21879; I have a work-around for that issue but still having a separate issue.

TypeScript Version: 2.8.0-dev.20180204

Code

declare function broke(impossible: never): never; // used to ensure full case coverage

interface Foo { kind: 'foo'; }
declare function isFoo(foobar: Foo | Bar): foobar is Foo;
interface Bar { kind: 'bar'; }
declare function isBar(foobar: Foo | Bar): foobar is Bar;

function mapFooOrBar<FooBar extends Foo | Bar, R>(
    foobar: FooBar,
    mapFoo: FooBar extends Foo ? ((value: Foo) => R) : ((impossible: never) => never),
    mapBar: FooBar extends Bar ? ((value: Bar) => R) : ((impossible: never) => never),
): R {
    if (isFoo(foobar)) {
        // workaround for #21879
        const handle = mapFoo as (value: Foo) => R;
        return handle(foobar);
    }
    else if (isBar(foobar)) {
        // workaround for #21879
        const handle = mapBar as (value: Bar) => R;
        return handle(foobar);
    }
    else {
        // workaround for #20375
        return broke(foobar as never);
    }
}

declare function toFoo(): Foo;

mapFooOrBar(
    toFoo(),
    foo => foo.kind,
/*  ^^^
Parameter 'foo' implicitly has an 'any' type. */
    bar => broke(bar)
/*  ^^^^^^^^^^^^^^^^^
                 ^^^
Argument of type '(bar: any) => any' is not assignable to parameter of type '(impossible: never) => never'.
  Type 'any' is not assignable to type 'never'. */
);

mapFooOrBar<Foo, string>(
    toFoo(),
    foo => foo.kind,
    bar => broke(bar)
);

(the casting foobar as never for the broke function is a workaround for #20375)

Expected Behavior
Compile without errors.

Actual Behavior
Typescript fails to infer the type of foo or bar in the callbacks, despite correctly inferring the type of the generic FooBar parameter in mapFooOrBar. Hovering over the first invocation of mapFooOrBar in VS Code, I see

function mapFooOrBar<Foo, {}>(foobar: Foo, mapFoo: (value: Foo) => {}, mapBar: (impossible: never) => never): {}

Which all looks correct, excepting the {} instead of string or 'foo' as the second parameter. But despite getting all that right, the parameters in the callback are inferred as any, which is likely the source of the {} second parameter. (Eliminating the second parameter and making the callbacks return void does not improve matters.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions