Skip to content

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

Closed as not planned
@Deathspike

Description

@Deathspike

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 :)

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