question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Built-in functions (e.g. isFinite) type guard and accepting null | undefined (strictNullChecks)

See original GitHub issue

TypeScript Version: 2.0.0

Code

// --strictNullChecks
interface IExample {
  value?: number;
}

let example: IExample = {};
if (isFinite(example.value)) { // Accept possibly `undefined`
  let a = example.value; // Narrow to `number`
}


Expected behavior:

  • isFinite should accept number | null | undefined rather than strictly number.
  • isFinite should act as a type guard.

Actual behavior: Won’t compile because isFinite is not accepting undefined and is not a type guard.

Proposal:

Change:

declare function isFinite(number: number): boolean;

To:

declare function isFinite(number: number | null | undefined): number is number;

I’m pretty sure there are other functions that could use the same love. I don’t mind putting in some hours if this is an accepted direction to take. Let me know 😃

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
RyanCavanaughcommented, Jul 30, 2016

isFinite(null) actually returns true (null gets coerced to 0)

JavaScript (╯°□°)╯︵ ┻━┻

It’s probably best to not allow null, then?

4reactions
mcaskillcommented, Jan 12, 2022

As far as I can understand this issue, my recent troubles with this issue, the current implementation of TypeScript (4.5.4), and the specifications of ECMAScript on the matter, both typings are wrong and should look something more like:

isFinite:

/**
 * Determines whether a supplied number is finite.
 * @param number Any numeric value.
 */
- declare function isFinite(number: number): boolean;
+ declare function isFinite(number: unknown): boolean;
  1. Let num be ? ToNumber(number).
  2. If num is NaN, +∞𝔽, or -∞𝔽, return false.
  3. Otherwise, return true.

19.2.2 isFinite, ECMA-262

NumberConstructor.isFinite:

/**
 * Returns true if passed value is finite.
 * Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a
 * number. Only finite values of the type number, result in true.
 * @param number A numeric value.
 */
- isFinite(number: unknown): boolean;
+ isFinite(number: unknown): number is number;
  1. If Type(number) is not Number, return false.
  2. If number is NaN, +∞𝔽, or -∞𝔽, return false.
  3. Otherwise, return true.

21.1.2.2 Number.isFinite, ECMA-262

Read more comments on GitHub >

github_iconTop Results From Across the Web

strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is true , null and undefined have their own distinct types and you'll get a type error if you try to...
Read more >
Why Number.isFinite dont have type guard? - Stack Overflow
isFinite dont have type guard like number is number ? THis example provides an error Object is possibly 'undefined' function inc (n?: number)...
Read more >
TypeScript - Type Guards For null and undefined - LogicBig
As we saw in Data types tutorial, TypeScript has two special types, null and undefined. These two types can have the values 'null'...
Read more >
https://raw.githubusercontent.com/microsoft/TypeSc...
TypeofNEFunction }); type TypeSystemEntity = Node | Symbol | Type ... for public members that accept a Node or one of its subtypes,...
Read more >
This copy of the TypeScript handbook was created on Monday ...
strictNullChecks. By default, values like null and undefined are assignable to any other type. This can make writing some code easier, but forgetting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found