Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type guards in Array.prototype.every #14963

Closed
kylewlacy opened this issue Apr 1, 2017 · 5 comments · Fixed by #38200
Closed

Type guards in Array.prototype.every #14963

kylewlacy opened this issue Apr 1, 2017 · 5 comments · Fixed by #38200
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@kylewlacy
Copy link

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)

@vkurchatkin
Copy link
Contributor

This issue is similar to #7657,

It's not the same, though. filter creates a new array, every doesn't. Assigning (string | number)[] to a number[] is unsafe, because a string can be pushed at any moment.

@kylewlacy
Copy link
Author

@vkurchatkin Would adding the above declaration for ReadonlyArray<T> be safe, then?

@vkurchatkin
Copy link
Contributor

@kylewlacy yes, I think so

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Apr 3, 2017
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 10, 2017
…rd if the callback is one.

Also made the type of thisArg generic.
Fixes microsoft#14963
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 10, 2017
…rd if the callback is one.

Also made the type of thisArg generic.
Fixes microsoft#14963
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 10, 2017
…rd if the callback is one.

Also made the type of thisArg generic.
Fixes microsoft#14963
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 11, 2017
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 18, 2017
NaridaL added a commit to NaridaL/TypeScript that referenced this issue Sep 19, 2017
@mhegazy mhegazy added this to the Community milestone Jan 4, 2018
@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2018

related to #5101

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2018

#16069 now tracks inferring the type guard automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants