Recently, TypeScript released the latest version of the language: V4.0. It comes with several features and enhancements available across the language itself and the tooling. So far, what it offers is quite promising. In this article, I am going to present five things that I love, comes with this release.

1. Editor improvements

As developers, we spend most of our time working with a code editor. I am a heavy VS Code user at the moment. Therefore I will start with the VS Code editor improvements for TS4. To use the latest editor functionalities, you have to switch the TS version in VS Code to the latest. Open command pallet and find the command TypeScript: Select Version and pick the newest version.

Image for post

Changing TS version in VS Code

Refactoring Hint to Convert to Optional Chaining

When working with nested objects, one of the most painful mistakes is to fail to check for undefined or null when accessing an object property. Let’s consider the following example:

// Approach 1
// This will break if the user is undefined or address
if(user.address.city){ }
// Approach 2
// Can be solved
if(user && user.address && user.address.city){ }
// Approach 3
// With optional chaining
if(user?.address?.city){ }

With TS4, it automatically suggests converting to optional chaining whenever it is applicable. I have been using Approach 2 for the most part, and that is how I used to start writing the code if it is accessing any inner object properties. Since converting to optional chaining is hinted, it is straightforward for me to write much readable code.

Image for post

Image for post

Convert to Optional Chaining hint in VS Code

Support for deprecated JSDoc comments

When it comes to using 3rd party libraries or modules, it is hard to notice if a Class or a Method has been deprecated unless we refer the documentation. With the new release, now it shows the syntax with a strikethrough text. Therefore it is easy to see which parts of your code use deprecated syntax.

#new-releases #typescript #javascript

5 Things I Love About The TypeScript 4.0 Release
1.30 GEEK