Skip to content

Narrowed types from user defined type guards are lost in subsequent method calls #20186

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 2.6.1.

Code

{
    // no error
    const arr1: Array<number> = ['foo', 1].filter(
        (x): x is number => typeof x === 'number',
    );

    /* unexpected error:
[ts]
Type '(string | number)[]' is not assignable to type 'number[]'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
*/
    const arr2: Array<number> = ['foo', 1]
        .filter((x): x is number => typeof x === 'number')
        .map(x => x);

    // workaround. no error
    const arr3: Array<number> = ['foo', 1]
        .filter((x: any): x is number => typeof x === 'number')
        .map(x => x);
}

Related ReactiveX/IxJS#162

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions