Nearly eight years ago Microsoft released the first public build, 0.8.1, of TypeScript, in November 2012. Since then it’s been used on many large-scale Web applications and in many apps running on the Electron cross-platform runtime. You’ve probably used one without realizing it, perhaps reading your e-mail on Outlook.com or working on some code in Visual Studio Code. One of Microsoft’s biggest open source projects, it’s led the way for the open sourcing of .NET and much of the current open design and development model used across the company. Now the latest version has been released, Version 4.0, with a significant number of improvements and new features.
Getting started with TypeScript 4.0
Microsoft’s philosophy with TypeScript has been to avoid breaking changes, so existing TypeScript applications can be transpiled with the new TypeScript tooling. All you need to do is upgrade using NuGet or npm to use it in any development environment that supports TypeScript. Microsoft offers deep integration in Visual Studio with a TypeScript 4.0 plug-in in the Visual Studio Marketplace that adds support for in-editor debugging and a command line compiler.
If you’re using Visual Studio Code you automatically get support for the newest release in an upcoming update, as it ships with a recent stable version. If you want the new TypeScript right away, you can either download an Insider Build of Visual Studio Code, switch to the nightly build of TypeScript from the Code command line, or add an updated TypeScript version number to your code and switch the workspace version in use.
This last approach might be your best option, as it lets you keep TypeScript 4.0 code separate from other versions you might need to maintain before upgrading. You may need to download an updated language server definition using npm and update your TypeScript user settings if you intend to make TypeScript 4.0 your default.
Working with types in arrays and tuples
This allows you to operate on the underlying arrays or tuples without knowing their types. The resulting code is easier to read and is still strictly typed under the hood, using the types you’ve used to define your variables or arrays. Functions written this way become more portable, as they don’t have to be customized for each specific operation. Other new tuple features help support inferring types that might not be fully defined, again making it easier to write portable functions that work on any array or tuple.
Another new feature helps improve code readability, as TypeScript 4.0 adds support for labeling tuple elements. You do need to label all the elements, and if they’re not labeled you will get an error. Labeled elements don’t affect the underlying code TypeScript produces, but they do make it easier to document and to use in-editor tools to help find errors. They help you understand what’s happening in arrays, making a set of labeled tuples look a lot more like a familiar parameter list.
The changes to how TypeScript manages tuples are probably the most important, as working with arrays is a common requirement in large-scale enterprise applications. They’re not the only changes. Others improve how TypeScript infers types in classes or add new ECMAScript features to the language, supporting new assignment operators to simplify your code.
Better code from better editor features
There are some breaking changes in TypeScript 4.0: For example, you can no longer use properties to override accessors in your base classes. In previous releases you’d only get an error if you did this under specific circumstances. Now TypeScript treats this as an error in all cases. Other changes include requiring optional operands for the delete operator and deprecating TypeScript’s older node factory functions in favor of a new API. These should only require minor changes to your code, clarifying a handful of inconsistencies and helping reduce the risk of errors and bugs.
Having static types in tuples and arrays should reduce errors, but the real benefit of TypeScript 4.0 comes from simplifying what would have been complex code into a structure that makes it easier to find and fix errors.
Copyright © 2020 IDG Communications, Inc.