2

Why does TypeScript not throw a type missmatch in the following?

undefined * 1 === NaN

Implementing a type-check for it would be trivial:

(n : number, m : number) => number

Would it also be possible to implement one for the + operator?

3
  • But undefined * 1 is a valid javascript statement, why would the compiler have any issues with that? Commented Jul 18, 2016 at 11:54
  • @NitzanTomer Sure, but personally I'd want TypeScipt to try to prevent me from ever producing NaN. Commented Jul 18, 2016 at 11:58
  • Because ... Javascript :( Commented Jul 18, 2016 at 18:21

2 Answers 2

2

Typescript is defined as a superset of JavaScript. Restricting that operation would forbid using otherwise valid JS code in TS.

Sure, but personally I'd want TypeScipt to try to prevent me from ever producing NaN

NaN is kind of a special case, because while "not a number" in the name, it still lies in the domain of the Number type (mostly because of underlying IEEE-754 implementation). Either way, you can construct your own stricter types (e.g. wrapping Number) if you need/want that.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't really safe guard against getting a NaN value based on the compiler, here's an example:

function mul(x: number, y: number): number {
    return x * y;
}

function getDataFromServer(): Promise<number> {
    // async request which returns a number
}

getDataFromServer().then(num => mul(num, 2));

The compiler has no way of knowing what the server will return.
It might be a number, it might be a string representation of a number ("2"), it might even be "internal server error".
You can check for those things at runtime in your code, but the compiler can't figure that statically.

4 Comments

I'm sorry, but I don't see how this answers the question. The implementation of getDataFromServer needs to guarantee that whatever server returned either forms a number, or signal promise error. That's how the compiler figures that statically. It has nothing to do with the OP asking about numerical operators not having stricter signatures.
@BartekBanachewicz As I wrote in my answer, the developer needs to make sure, inside the implementation of getDataFromServer that it returns a number, but I was trying to show that the compiler can't possibly know whether it will be a number, NaN or something else.
Well, so what? The data from a socket will be typically typed as a "buffer" or a "string" (which you could create by other means). What does that have to do with mathematical operators' signatures?
Well, your answer was answered by me as a comment: "undefined * 1 is a valid javascript statement" which suffice, but I also wanted to show the OP that the compiler can NOT know that, it's a runtime thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.