I have a function for comparison that has a union return type. It can return -1, 1, or 0. However, I need to handle a special case when at least one of the items being compared is not defined. While the compiler allows me to include null as a potential return value, it does not allow NaN (which could make sense in certain scenarios such as comparing numbers or dates).
The following code compiles successfully:
function myFunc(item1: MyType, item2: MyType): -1 | 0 | 1 | null {
...
}
However, when I try to add NaN as a possible return value like this:
function myFunc(item1: MyType, item2: MyType): -1 | 0 | 1 | NaN {
...
}
The compiler throws an error saying "Cannot find name NaN". Why is NaN not allowed? Is there any workaround to utilize NaN in this context?