TypeScript and Functions
Another huge benefit of TypeScript is automatic function invocation checking. Let's say that we have the function we used for our first TypeScript file:
function add (x, y) { return x + y; }
Even without any type annotations, TypeScript still has some information about this function – namely, that it takes two, and exactly two, parameters.
In contrast, JavaScript does not enforce that the number of actual arguments has to conform to the number of parameters defined, so all of the following invocations are valid calls in JavaScript:
add(1, 2); // two arguments add(1, 2, 3); // three arguments add(1); // one argument add(); // no arguments
In JavaScript, we can call a function with more arguments than parameters, fewer arguments, or even without any arguments at all. If we have more arguments than needed, the extra arguments are simply ignored (and stored in the magical arguments
variable), and if we have...