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.

You actually can get Infinity type if you're clever and other fun floating point oddities

See original GitHub issue

As explained in #15135 and #15351, the Typescript team doesn’t intend to support NaN and Infinity literals, due to complexity. Explicitly typing something as Infinity or -Infinity gives the error:

'Infinity' refers to a value, but is being used as a type here.

As it turns out, you actually can get the Infinity type fairly easily. Just use a really large number that parses to infinity as your type, like 1e999. -1e999 gives you negative Infinity. Fortunately, I don’t believe you can get NaN from just parsing a double precision literal other than NaN itself. 0/0 is the simplest way to get NaN, but that’s 2 literals and a divide.

There are a number of other floating point literal peculiarities resulting from round-off as well. I fully expect the Typescript team to “won’t fix” them:

type DoesntExtend = 1.000000000000001 extends 1 ? 'yes' : 'no' // Resolves to 'no'
type Extends      = 1.0000000000000001 extends 1 ? 'yes' : 'no' // Resolves to 'yes'

Here’s another.

type NegativeZero = -0 extends 0 ? 'yes' : 'no'; // Resolves to 'yes'

In this case, -0 and 0 are actually distinct numbers with different bit patterns. Both Chrome and Firefox dev consoles correctly echo -0 as -0 rather than 0. However, the IEEE-754 standard defines that 0 === -0, so I’m not surprised Typescript thinks they’re the same type. The intent for this distinction is to convey the idea of limits as a numeric representation (not to mention floating point is sign-magnitude anyways, so why not embrace it). For example, 1/-Infinity and -8*0 both result in -0 rather than 0. For a type system, I’m not sure that you strictly need or want to maintain this distinction.

TypeScript Version: Repro’d on playground

Search Terms: Infinity, NaN Code

type Illegal = Infinity; // Doesn't compile
type Inf = 1e999; // Resolves to Infinity
type NegativeInf = -1e999; // Resolves to -Infinity

Expected behavior:

Actual behavior:

Playground Link: Here

Related Issues:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

23reactions
jcalzcommented, Jun 4, 2019

Infinity will not satisfy Inf

🤔

// THIS CODE IS EVIL AND YOU SHOULD RUN AWAY FROM IT
type Infinity = 1e999;
if (!((x: any): x is Infinity => true)(Infinity)) throw new Error();

const a: Infinity = Infinity; // okay now
7reactions
AnyhowStepcommented, Jun 5, 2019

I don’t care what the warnings are. I want those NaN and Infinity literals. I also want a “finite number” type.

I’m going to find some way to shoehorn that Infinity trick into my code and see if anything explodes

Read more comments on GitHub >

github_iconTop Results From Across the Web

When is equality transitive? and other floating point curiosities
According to the docs, you can explicitly cast to an int type. ... But if we promote the int to a float, distinct...
Read more >
How many kinds of infinity are there? - YouTube
A lot.List with links: http://vihart.com/how-many- kinds -of- infinity -are-there/
Read more >
Floating Point Precision - AdoredTV
It represents a sequence of digits that goes on forever. Forever is a very long time. Computers are finite machines, built with finite...
Read more >
A crash course on floating point numbers in a computer, Part III
Another thing we take for granted is the conversion between different numeric types. Sometimes we need to convert from an integer to floating...
Read more >
Why does PostgreSQL consider NULL boundaries in range ...
But again, NULL boundaries and infinite boundaries work exactly the same when we're dealing with the actual elements that make up the range....
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