Short version: TypeScript is not type safe.
After all this hard work, you’re still vulnerable to the pitfalls and failures of TypeScript. The following code should not pass a type check because
HTMLCollection in Gecko and
NodeList for every other browser engine:
[js]var foo:NodeList = document.getElementsByTagName("*");[/js]
However, we can still make the test pass simply by adding type annotations to the DOM and signifying to the compiler that
getElementsByTagName always returns
NodeList for simplicity’s sake. Unfortunately, that creates a false sense of security, and we’re stuck with scenarios which lead the programmer to expect the following code to return
[js]document.getElementsByTagName("*") instanceof NodeList[/js]
The above will return
true in every other browser but Gecko-based browsers (namely, Firefox) because, once again, Mozilla insists
HTMLCollection should be returned as defined in the specification. The plot thickens and the programmer is now writing code and using the methods of
NodeList which result in runtime
TypeErrors. In most cases, bugs like this can be fixed, but Mozilla has filed this bug as WONTFIX for the aforementioned reason:
If Mozilla wanted to fix it, they would have fixed it in 1999 (when the bug was first filed).
Nevertheless, even in a perfect world where this were consistent across all web browsers, it doesn’t address the underlying problem: there are too many inconsistencies across browsers, too many edge cases leaving a language/compiler perpetually in the “beta” stage, and there is just enough infighting and disagreement amongst browser vendors to prevent the building of a solution that’s robust and enterprise-ready.
Static type checkers only check types at compile time; they do not simulate or evaluate code. Therefore, it’s impossible to distinguish a
NaN. In JS++, when you want an
int, you get an
int, and not a number of type
Microsoft TypeScript can be downright dangerous because it gives you a false sense of security, which is worse than having no security at all. I’m surprised Microsoft allowed their brand to be associated with this. Anders Hejlsberg has my highest respect; I began as a programmer on Delphi 3, and I learned C# when it first came out and fell in love with it. I’m all too familiar with how Gates personally recruited him to join Microsoft. However, he is not the right person for this job because he simply does not have the requisite experience to draw upon to create the best possible developer experience.