NaN, Infinity and -Infinity not accepted in number literal types #15135
Labels
Suggestion
An idea for TypeScript
Too Complex
An issue which adding support for may be too complex for the value it adds
TypeScript Version: 2.2.2
Code
Expected behavior:
Well, NaN, +Infinity and -Infinity are valid numbers, so I see no reason they should not be accepted
Actual behavior:
It fails to compile
** Benefits **
I discovered this was all discussed in #9407. @ahejlsberg and @weswigham explicetly took position against including it, except for a compelling case...
Other than being the intuitive result (why on earth a perfectly valid number would not be acceptable a in a number literal type, I see several benefits :
One application would be the Math.sign() type definition. It is currently defined as returning a
number
although the spec says it can only return0 | -0 | +1 | 1 | NaN
This is in fact how I got into the problem : I wanted to implement a comparator function (returning -1|0|1) using Math.sign( ), intuitively expecting to be limited to those 3 values (and at the time forgetting about NaN)
To continue with the Math.sign, it could be defined as follows
This way, it explicitely states when the function returns NaN and be extremely useful for documentation purposes (I suppose modifying a compiler for documentation is not very strong...)
Another one I can see (although it's a quick thought) : I think it could provide very interesting handling of NaN (if we had substraction types #4183)
The text was updated successfully, but these errors were encountered: