Integer (literal) union type, with NaN as valid return value

Viewed 665

I have a (comparison) function with an union return type. It can return -1, 1 or 0. However, I need a special case ("result") for when at least one of the items being compared is not defined. The compiler allows me to add null as a potential return value, but not NaN (which, in some scenarios would make sense, e.g. comparing numbers or dates).

This compiles

function myFunc(item1: MyType, item2: MyType): -1 | 0 | 1 | null {
   ...
}

but for this one, the compiler says it "Cannot find name NaN":

function myFunc(item1: MyType, item2: MyType): -1 | 0 | 1 | NaN {
   ...
}

Why is NaN not allowed? Is there a way to use NaN?

2 Answers
Related