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.

Broken emit when `Infinity` or `‑Infinity` ends up in a type position

See original GitHub issue

Bug Report

🔎 Search Terms

  • Infinity
  • NaN
  • numeric
  • literal

🕗 Version & Regression Information

💻 Code

// @declaration
// @showEmit
// @showEmittedFile: index.d.ts

// @filename: index.ts
export const PositiveInfinity: 1e1_000_000 = 1/0 as any;
export const NegativeInfinity: -1e1_000_000 = -1/0 as any;

⏯ Playground Link

Workbench Repro

🙁 Actual behavior

TypeScript emits Infinity and ‑Infinity in a type position, which are intentionally invalid according to https://github.com/microsoft/TypeScript/pull/9407#issuecomment-229721835:

// index.d.ts
export const PositiveInfinity: Infinity;
export const NegativeInfinity: -Infinity;

🙂 Expected behavior

The Infinity and ‑Infinity values are valid in a type position, so that the generated .d.ts file is valid.


Also, it’d be nice to support literal NaNs, which would allow for Number.isNaN to be typed as:

interface NumberConstructor {
	isNaN(number: unknown): number is NaN;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Jamesernatorcommented, Feb 22, 2021

In your example you could just as well replace the Infinity with undefined. Having a dedicated Infinity type would not help you in this case, because Infinity would be a sub-type of number, so the union type number | Infinity would be simplified to number.

The example is bigint not number. BigInts don’t contain an Infinity value, I think it’s a lot more readable to use the explicit Infinity value rather than some arbitrary token as it already compares correctly with bigints (e.g. bigint < Infinity is always true, no coercion to Number happens).

2reactions
MartinJohnscommented, Feb 21, 2021

You’re mixing a bug report and a feature request together in one. Fixing the bug would be to emit the type number instead of Infinity. Introducing an Infinity (and NaN) type is a feature request, for which you should provide a good reasoning in response to https://github.com/microsoft/TypeScript/pull/9407#issuecomment-229721835.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinity & NaN Type-level Support #51741 - GitHub
Successfully merging this pull request may close these issues. Broken emit when Infinity or ‑Infinity ends up in a type position ...
Read more >
Hydrogen's Atomic Emission Spectrum - Chemistry LibreTexts
This is known as its ground state. If enough energy is supplied to move the electron up to the infinity level, the atom...
Read more >
atom - Orbits and energy levels | Britannica
An electron disappears from the orbit in which it is located and reappears in its new location without ever appearing any place in...
Read more >
UNSC Infinity - Ship - Halopedia, the Halo wiki
UNSC Infinity is an experimental Infinity-class supercarrier of the UNSC Navy commissioned following the end of the Human-Covenant War.
Read more >
3.5 Limits at Infinity, Infinite Limits and Asymptotes
If this limits exists, we say that the function f f has the limit L L as x x increases without bound. Similarly,...
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