Skip to content

Type guards in Array.prototype.every #14963

@kylewlacy

Description

@kylewlacy

TypeScript Version: 2.2.2

Code

function isNumber(x: any): x is number { return typeof(x) === "number"; }

let numbersOrStrings: (number | string)[] = [1, 2, 3, 4];

if(numbersOrStrings.every(isNumber)) {
    let _numbers: number[] = numbersOrStrings;
    // ...
}

Expected behavior:

When every member of numberOfStrings matches the isNumber type guard, the whole array should be inferred to be numberOfStrings: number[]; so, the above code should compile without error.

Actual behavior:

The above fails to compile, with the following error:

error TS2322: Type '(string | number)[]' is not assignable to type 'number[]'.

This issue is similar to #7657, and it can be solved by adding something like the following to lib.d.ts:

interface Array<T> {
    every<U extends T>(pred: (a: T) => a is U): this is U[];
}

(or, the above can be added locally to a project as a temporary workaround)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions