-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow Inifinity and -Infinity as number literal types #32277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Also, to state the obvious in favour of this, Quizzically, type numberNames = {0: "zero", 1: "one", Infinity: "inf"};
type numberTypes = keyof numberNames;
// These are fine:
var zero:numberTypes = 0;
var one:numberTypes = 1;
// This is an expected error:
var two:numberTypes = 2;
/* ERROR
var two: 0 | 1 | "Infinity"
Type '2' is not assignable to type '0 | 1 | "Infinity"'.
*/
// This is an unexpected error:
var inf:numberTypes = Infinity;
/* ERROR
var inf: 0 | 1 | "Infinity"
Type 'number' is not assignable to type '0 | 1 | "Infinity"'.
*/ Here, So, |
const Inf = 999e308;
type Infinity = 999e999999; //Just for fun, it's still the same as `Inf`
//We use `Inf` instead of `Infinity`
type numberNames = {0: "zero", 1: "one", [Inf]: "inf"};
type numberTypes = keyof numberNames;
// These are fine:
var zero:numberTypes = 0;
var one:numberTypes = 1;
// This is an expected error:
var two:numberTypes = 2;
/* ERROR
var two: 0 | 1 | "Infinity"
Type '2' is not assignable to type '0 | 1 | "Infinity"'.
*/
// OK!
var inf:numberTypes = Infinity as Infinity; |
I have a very similar use case and was surprised that this doesn't work. |
Also prevents you from making Infinity a mandatory key in a export type ImageSrcSet = Record<number, string> & { [Infinity]: string }; I'd expect that to resolve to a numeric dictionary where all keys are optional except for |
I agree it makes sense to add |
Additional silly (?) use case: Consider a roleplaying game where players roll a dice against an unconstrained difficulty number. You'd like a type that includes the possible die results plus an 'auto-succeed' value for peculiar circumstances, e.g. type D6 = 1|2|3|4|5|6|Infinity |
This comment has been minimized.
This comment has been minimized.
I also ran into this when implementing unbounded ranges, hoping that |
I have been waiting for this for over 4 years. Unless I'm missing something, (-)
|
My usecase for Infinity is a "flatten" utility accepting a depth of number including Infinity. It would be nice to overload for the case of |
As long as Infinity exists in JavaScript, I think people will find reasonable use cases for it from time to time, and TypeScript is limiting its utility for no good reason. Today I wanted to write a function like
|
I have code/comments like the following for years. Would love to finally be able to do it the "right" way.
|
The
At the bit level,
Similarly, This means that |
Please accept
Infinity
and-Infinity
as valid number literal types.Currently, This causes a compile error,
'Infinity' refers to a value, but is being used as a type here. (2749)
.I understand this was once rejected as part of #15135 and #15356, and the given reasons were: 1) difficult to implement, and 2) lacks a compelling use case.
However, while I understand why
NaN
as a literal type is tricky to implement,NaN
andInfinity
are not the same. And I have a clear use case forInfinity
as a meaningful literal type, as shown above.Infinity is not tricky to compare
Unlike notorious
NaN
or-0
,Infinity
works just like an ordinary numeric constant as far as equality is concerned. We don't need a special function likeisNaN()
orObject.is()
. Ever since the ES5/IE6 era, there has been nothing counter-intuitive:Most importantly,
Infinity === Infinity
is true (whileNaN === NaN
is false). Unless I'm missing something, predictable equality is all that's required to safely useInfinity
as a literal type, right? Even though the design note (#15356) says "there is more than one NaN, Infinity, etc", you can think ofInfinity
in JavaScript as "just a fixed number" which happens to be larger thanNumber.MAX_VALUE
.My use case
My library deals with unbounded (aka infinite) integer ranges, and I have been using
Infinity
and-Infinity
without any issue to denote what they literally mean, infinity in the mathematical sense. I have instructed my users to use these interesting constants, too. Recently I started to extend my library to supportbigint
in addition tonumber
, and ran into this problem.You may ask "Why don't you just use string
'Infinity'
orSymbol('unbounded')
", butInfinity
is a predefined and predictable constant, and it can be directly compared with any bigint (e.g.,10n ** 1000n < Infinity
istrue
). See how simple the implementation ofisValid
can be in the first example.PS: Looks like there is a hacky workaround (#31752), but I'd like to see the official support.
The text was updated successfully, but these errors were encountered: