Skip to content

Infinities as number literal types #56272

Description

🔎 Search Terms

infinity number literal

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20231031#code/C4TwDgpgBACg9gZwJbCQNwgSQHYDMnYohQC8UAjBAJw0DcAUAPSNSsB6A-PaJFAHIQA5gENUGHPkKhSUALSUaVBs1ZRO9IA

💻 Code

type PositiveInfinity = 1e999;
//   ^?
type NegativeInfinity = -1e999;
//   ^?

🙁 Actual behavior

In TS 5.2 we get NumberLiteralTypes with Infinity and -Infinity values.

In the upcoming TS 5.3 we get a number type in the of the PositiveInfinity

🙂 Expected behavior

I don't know. I created this issue to discuss exactly that.

Should this part of the change be reverted and should Infinity be allowed as a literal number value? Or should -Infinity be aligned with Infinity and should it be converted~ to a number type as well?

Additional information about the issue

I didn't see an Infinity as a literal type so I made this change while working in this area. Ron Buckton (@rbuckton) commented here:

I really hope no one is relying on this.

but it turns out that some try to rely on this. We can see it in this issue, cc Max (@Max10240)

Activity

  1. jcalz commented on Oct 31, 2023

    @jcalz
    Contributor

    Cross-linking #32277 and #31752

  2. weswigham commented on Nov 2, 2023

    @weswigham
    Member

    Also see the original PR and my original PR. My first version had NaN and Infinity types - they're not hard to add if you implement them as compiler internal symbols - Anders Hejlsberg (@ahejlsberg) wanted the compelling usecases for them enumerated before we'd add them, though. Not sure anyone's going to be happy if the only usecase is arithmetic in the typesystem...

  3. fatcerberus commented on Nov 5, 2023

    @fatcerberus

    Not sure anyone's going to be happy if the only usecase is arithmetic in the typesystem...

    Or negated types. number & !NaN, etc.

  4. ExE-Boss commented on Dec 3, 2023

    @ExE-Boss
    Contributor

    Also cross‑linking: #28682, #42905, and #51741

  5. k4b7 commented on Dec 24, 2024

    @k4b7

    I'm writing a library that solves algebraic equations and wanted to use Infinity in a enum to model the number of solutions to a linear equation, e.g.

    type NumberOfSolutions =  0 | 1 | Infinity;
    

    I can work around this limitation but creating an enum, but being able to use Infinity as a type literal seemed like an elegant way to model the problem.

  6. ExE-Boss commented on Jan 27, 2025

    @ExE-Boss
    Contributor

    @kevinbarabash
    For your case, you can replace Infinity with 1e1_000_000 which becomes Infinity in the type system, but you’ll run into #42905 if you emit .d.ts files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions