What I Am Excited About in TypeScript 4.0

What I Am Excited About in TypeScript 4.0

What I Am Excited About in TypeScript 4.0. Let's take a look at the recent 4.0 release and see why! 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.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

TypeScript vs. JavaScript Comparison – Pros, Cons, Trends

We'll look at the pros, cons and trends of TypeScript compared to JavaScript. What has driven so many developers away from JavaScript and into TypeScript? Let’s make a side-by-side TypeScript vs JavaScript comparison.