What is TypeScript, and why you should use it?

What is TypeScript?

Both the TypeScript home page and Nimesh Sakhiya’s LinkedIn article offer insights into what TypeScript is and highlight its advantages.

Why should you use TypeScript?

Case 1: Misspelled property

const blueTriangle = { base: 10, height: 5 };

function calculateTriangleArea(triangle) {
    return (triangle.base * triangle.heigth) / 2;
}

console.log(calculateTriangleArea(blueTriangle));

This will log ‘NaN’ instead of the expected area.

Case 2: Unexpected type coercion

function sumNumbers(a, b) {
    return a + b;
}

const r1 = sumNumbers(17, 6);
const r2 = sumNumbers("17", "6");
const r3 = sumNumbers(17, "6");
const r4 = sumNumbers(17, "");
const r5 = sumNumbers(17, []);

The results will be: r1 = 23, r2 = "176", r3 = "176", r4 = "17", r5 = "17"

Case 3: Calling a method on an unexpected type

const message = "Hello World!";
console.log(message.toUpperCase());
message();