Now is the best time to adopt TypeScript. Let’s take a look at the recent 4.0 release and see why!

TypeScript

TypeScript, a superset of JavaScript, is an open-source, strongly-typed, object-oriented compiled language developed and maintained by the team at Microsoft. TypeScript ensures that even before you save your code it is type-checked, and then after that even strips away all the types to have a clean and legible JavaScript. It is also used in your favorite editors to do things like auto-complete and refactoring. TypeScript has one of the fastest-growing developer communities around, and with the new version 4.0, there are no major breaking changes, so this is the best time to get started with TypeScript.

Some Background

TypeScript has recorded tremendous growth in the past few months with monthly download numbers on npm surpassing 50 million in July 2020. It is clear that there is great love for TypeScript. Last year the The State of JavaScript developer survey had thousands of developers vote TypeScript as their second most loved language, and almost 90% say they will use it again.

Installing TypeScript

For this new version, TypeScript developers can get it through NuGet at this link, or use the Node package manager with this command:

npm install -D typescript

IDE support for TypeScript for this version is already on Visual Studio and even Visual Studio Code by installing the Insiders version on the link here.

This version of the language focused on a deeper dive into expressivity, productivity, and scalability and a lot of improvements on the experience around tuples. Now let’s take a look at an overview of some of the changes this new version shipped with.

Short-Circuiting Assignment Operators

In JavaScript you can rewrite the following commands as below for simple operations like addition, subtraction, division and multiplication:

// Addition
// a = a + b
a += b;
// Subtraction
// a = a - b
a -= b;
// Multiplication
// a = a * b
a *= b;
// Division
// a = a / b
a /= b;
// Exponentiation
// a = a ** b
a **= b;
// Left Bit Shift
// a = a << b
a <<= b;

TypeScript supports these too and now that ECMAScript is also progressing and adding new assignment operators like &&=, ||=, and ??= this new version shipped with support for them too. A community member Wenlu Wang contributed this new feature.

These operators are good for swapping and even complying to the DRY principle. An example for use would be where a user might write code like the following:

a = a && b;
a = a || b;
a = a ?? b;

Or even an if code block like this one below:

// could be 'a ||= b'
if (!a) {
    a = b;
}

To start using it, consider running this example below to see how that differs from always performing the assignment.

const obj = {
    get prop() {
        console.log("getter has run");
        // Replace me!
        return Math.random() < 0.5;
    },
    set prop(_val: boolean) {
        console.log("setter has run");
    }
};
function foo() {
    console.log("right side evaluated");
    return true;
}
console.log("This one always runs the setter");
obj.prop = obj.prop || foo();
console.log("This one *sometimes* runs the setter");
obj.prop ||= foo();

#typescript #javascript #programming #developer #web-development

What I Am Excited About in TypeScript 4.0
1.65 GEEK