-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this
Milestone
Description
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)
tinganho, krk, screendriver, th4t-gi, lorenzodallavecchia and 6 more
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this