A tiny bit of type safety can't hurt

To wrap this chapter up, let's write another example. This time, we will write an unsafe program and then we will see how we can make it safer using TypeScript.

Our program will be a simple calculator. For the sake of the example, our calculator will only be able to multiply values together, but don't hesitate to extend the example to include other operations as well.

Create a new file and call it calculator.ts. In that file, add the following code:

function multiply(a, b) {  const result = a * b;  console.log("The multiplication of "+a+"*"+b+" equals to :    "+result);  return result;}multiply(1, 2);multiply(2,2);multiply(-10,10);//multiply('foo', "bar");

We've used const this time because the calculation ...

Get Learn TypeScript 3 by Building Web Applications now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.