Skip to content

"Number.isFinite" and "Number.isInteger" do not filter out values as a predicate #39090

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

Closed
goodwin64 opened this issue Jun 16, 2020 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@goodwin64
Copy link

TypeScript Version: 3.9.2

Search Terms: isFinite, isInteger, filter, callback, predicate, null values

Code

const nums = [1, 2, 3, null, 5];
const filteredFinite = nums.filter(Number.isFinite);
const filteredIntegers = nums.filter(Number.isInteger);

Expected behavior:

const filteredFinite: number[]
const filteredIntegers: number[]

Actual behavior:

const filteredFinite: (number | null)[]
const filteredIntegers: (number | null)[]

Playground Link:
https://www.typescriptlang.org/play/?ssl=2&ssc=7&pln=2&pc=21#code/MYewdgzgLgBGCuBbCMC8MDaBGANDATHgMx4IA2ZeArALoDcAsAFCiSwBmAlmVAKYBOvACYAxTmE580cJBAB0XHgIAUAOSQAjAXM4QxEvgEpGLcNBiK+goQEkwfAOYCU6BMgXcrazdt13HAsbMzKwQIGS8cmQgDsqWAsL6krxBppDhkdGx8db+vE78EKlAA

Related Issues: not found

@fatcerberus
Copy link

fatcerberus commented Jun 16, 2020

As far as TS is concerned, it's just like any other function, a black box that returns true or false. There's no special case in the compiler that says using Number.isFinite in particular will remove all the nulls.

edit: I forgot type predicates are a thing, ignore the above brainfart.

Note that the function doesn't work as a type predicate in general either:

let x: number | null = 777 as any;
if (Number.isFinite(x)) {
    x;  // still number | null
}

The fix would be for these functions to be declared as predicates, e.g.

    isFinite(number: unknown): number is number;

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 16, 2020
@RyanCavanaugh
Copy link
Member

Type guards are only correctly type guards if they return true for every value of the tested type, not some values of the type.

See #15048 for supporting type guards that only identify a subset of their type.

@fatcerberus
Copy link

fatcerberus commented Jun 16, 2020

Ah right, because otherwise !Number.isInteger(x) implies that x is not a number and would be narrowed as such. I forgot that that works both ways when I made the above suggestion.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants