Agentic hosting is here. Connect to our MCP now ->

Blog

What's new in TypeScript 5.0: decorators, const type parameters, enum improvements, speed, and much more!

Decorators are a way to customize the behavior of classes and their members in a reusable way.

ยทby Joel Olawanle

TypeScript 5.0 was officially released on March 16, 2023. This release introduces many new features with the aim of making TypeScript smaller, simpler, and faster.

This new release modernizes decorators for class customization, allowing for the customization of classes and their members in a reusable way. Developers can now add a const modifier to a type parameter declaration, allowing const-like inferences to be the default. The new release also makes all enums union enums, and it makes the compiler faster and its package smaller.

In this article, you will explore the changes introduced in TypeScript 5.0, providing an in-depth look at its new features and capabilities.

TypeScript has had several releases since 5.0, and the current major version, TypeScript 7.0, moves the compiler to a native implementation for much faster builds. Every feature in this article is still part of TypeScript today, and all the examples compile with both TypeScript 5.0 and 7.0.

Getting started with TypeScript 5.0

TypeScript ships as a package you can install into your project using npm. To use the features covered here, you need TypeScript 5.0 or later. Run the following command in your project's directory to install the latest version:

npm install -D typescript

This will install the compiler in the node_modules directory, which you can now run with the npx tsc command.

If you use TypeScript in Visual Studio Code, you can tell the editor to use the version you installed in your project. The VS Code documentation explains how.

What's new in TypeScript 5.0?

Let's explore five major updates introduced in TypeScript 5.0.

Modernized decorators

Decorators have been around in TypeScript for a while under an experimental flag, but the new release brings them up to speed with the ECMAScript decorators proposal. The proposal reached stage 3, which means its design is complete and it's ready to be implemented. TypeScript typically adds new JavaScript features once they reach this stage.

Decorators are a way to customize the behavior of classes and their members in a reusable way. For example, if you have a class that has two methods, greet and getAge:

class Person {
    name: string;
    age: number;
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
 
    greet() {
        console.log(`Hello, my name is ${this.name}.`);
    }
 
    getAge() {
        console.log(`I am ${this.age} years old.`);
    }
}
 
const p = new Person('Ron', 30);
p.greet();
p.getAge();

In real-world use cases, this class should have more complicated methods that handle some async logic and have side effects, etc., where you would want to throw in some console.log calls to help debug the methods.

class Person {
    name: string;
    age: number;
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
 
    greet() {
        console.log('LOG: Method Execution Starts.');
        console.log(`Hello, my name is ${this.name}.`);
        console.log('LOG: Method Execution Ends.');
    }
 
    getAge() {
        console.log('LOG: Method Execution Starts.');
        console.log(`I am ${this.age} years old.`);
        console.log('LOG: Method Execution Ends.');
    }
}
 
const p = new Person('Ron', 30);
p.greet();
p.getAge();

This is a frequently occurring pattern, and it would be convenient to have a solution to apply to every method.

This is where decorators come into play. We can define a function named debugMethod that appears as follows:

function debugMethod(originalMethod: any, context: any) {
    function replacementMethod(this: any, ...args: any[]) {
        console.log('LOG: Method Execution Starts.');
        const result = originalMethod.call(this, ...args);
        console.log('LOG: Method Execution Ends.');
        return result;
    }
    return replacementMethod;
}

In the code above, the debugMethod takes the original method (originalMethod) and returns a function that does the following:

  1. Logs the message "LOG: Method Execution Starts."
  2. Calls the original method with all its arguments and the same this.
  3. Logs the message "LOG: Method Execution Ends."
  4. Returns whatever the original method returned.

By using decorators, you can apply the debugMethod to your methods as shown in the code below:

class Person {
    name: string;
    age: number;
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    @debugMethod
    greet() {
        console.log(`Hello, my name is ${this.name}.`);
    }
    @debugMethod
    getAge() {
        console.log(`I am ${this.age} years old.`);
    }
}
const p = new Person('Ron', 30);
p.greet();
p.getAge();

This will output the following:

LOG: Method Execution Starts.
Hello, my name is Ron.
LOG: Method Execution Ends.
LOG: Method Execution Starts.
I am 30 years old.
LOG: Method Execution Ends.

When defining the decorator function (debugMethod), a second parameter is passed called context (the context object, which has useful information about how the decorated method was declared, including the name of the method). You can update your debugMethod to get the method name from the context object:

function debugMethod(
    originalMethod: any,
    context: ClassMethodDecoratorContext
) {
    const methodName = String(context.name);
    function replacementMethod(this: any, ...args: any[]) {
        console.log(`'${methodName}' Execution Starts.`);
        const result = originalMethod.call(this, ...args);
        console.log(`'${methodName}' Execution Ends.`);
        return result;
    }
    return replacementMethod;
}

When you run your code, the output will now carry the name of each method that is decorated with the debugMethod decorator:

'greet' Execution Starts.
Hello, my name is Ron.
'greet' Execution Ends.
'getAge' Execution Starts.
I am 30 years old.
'getAge' Execution Ends.

There is more to what you can do with decorators. Feel free to check the original pull request for more information on how to use decorators in TypeScript.

Introducing const type parameters

This is another big feature that gives you a new tool with generics in order to improve the inference that you get when you call functions. By default, when you declare an array with const, TypeScript infers a general type and not its literal values:

// Inferred type: string[]
const names = ['John', 'Jake', 'Jack'];

Until now, to achieve the desired inference, you had to use the const assertion by adding "as const":

// Inferred type: readonly ["John", "Jake", "Jack"]
const names = ['John', 'Jake', 'Jack'] as const;

When you call functions, it's similar. In the code below, the inferred type of countries is string[]:

type HasCountries = { countries: readonly string[] };
function getCountriesExactly<T extends HasCountries>(arg: T): T['countries'] {
    return arg.countries;
}
 
// Inferred type: string[]
const countries = getCountriesExactly({ countries: ['USA', 'Canada', 'India'] });

You may want a more specific type, and before TypeScript 5.0, the way to get it was to add the as const assertion:

// Inferred type: readonly ["USA", "Canada", "India"]
const countries = getCountriesExactly({ countries: ['USA', 'Canada', 'India'] } as const);

This can be difficult to remember and implement. However, TypeScript 5.0 introduces a new feature where you can add a const modifier to a type parameter declaration, which will automatically apply a const-like inference as default.

type HasCountries = { countries: readonly string[] };
function getCountriesExactly<const T extends HasCountries>(arg: T): T['countries'] {
    return arg.countries;
}
 
// Inferred type: readonly ["USA", "Canada", "India"]
const countries = getCountriesExactly({ countries: ['USA', 'Canada', 'India'] });

Using const type parameters allows developers to express intent more clearly in their code: the function author decides that callers get the most specific types, so callers don't have to remember as const. Keep in mind that it only changes type inference. It doesn't make the values immutable at runtime. The result is readonly here because the constraint uses readonly string[].

You can check the original pull request for more information on how the const type parameter works in TypeScript.

Improvements to enums

Enums in TypeScript are a powerful construct that allows developers to define a set of named constants. In TypeScript 5.0, improvements have been made to enums to make them even more flexible and useful.

For example, if you have the following enum passed into a function:

enum Color {
    Red,
    Green,
    Blue,
}
 
function getColorName(colorLevel: Color) {
    return colorLevel;
}
 
console.log(getColorName(1)); // OK: 1 is Color.Green
console.log(getColorName(4)); // Error in TypeScript 5.0 and later

Before TypeScript 5.0, you could pass a number that doesn't match any member, like 4, and the compiler would report no error. Since TypeScript 5.0, the compiler reports an error: Argument of type '4' is not assignable to parameter of type 'Color'.

Also, the new release makes all enums into union enums by creating a unique type for each computed member. This enhancement allows for the narrowing of all enums and the referencing of their members as types:

enum Color {
    Red,
    Purple,
    Orange,
    Green,
    Blue,
    Black,
    White,
}
 
type PrimaryColor = Color.Red | Color.Green | Color.Blue;
 
function isPrimaryColor(c: Color): c is PrimaryColor {
    return c === Color.Red || c === Color.Green || c === Color.Blue;
}
 
console.log(isPrimaryColor(Color.White)); // Outputs: false
console.log(isPrimaryColor(Color.Red)); // Outputs: true

Performance improvements of TypeScript 5.0

TypeScript 5.0 includes numerous significant changes in code structure, data structures, and algorithmic extensions. This has helped improve the entire TypeScript experience, from installation to execution, making it faster and more efficient.

For example, the difference between the package size of TypeScript 5.0 and 4.9 is quite impressive.

With this release, TypeScript's own codebase migrated from namespaces to modules, allowing it to leverage modern build tooling that can perform optimizations like scope hoisting. Together with removing some deprecated code, this shaved off about 26.4 MB from TypeScript 4.9's 63.8 MB package size.

TypeScript package size
TypeScript package size

Here are a few more interesting wins in speed and size between TypeScript 5.0 and 4.9:

ScenarioTime or size relative to TS 4.9
material-ui build time90%
TypeScript Compiler startup time89%
Playwright build time88%
TypeScript Compiler self-build time87%
Outlook Web build time82%
VS Code build time80%
typescript npm Package Size59%

Bundler resolution for better module resolution

When you write an import statement in TypeScript, the compiler needs to know what the import refers to. It does this using module resolution. For example, when you write import { a } from "moduleA", the compiler needs to know the definition of a in moduleA to check its use.

In TypeScript 4.7, two new options were added for the --module and moduleResolution settings: node16 and nodenext.

The purpose of these options was to more accurately represent the exact lookup rules for ECMAScript modules in Node.js. However, this mode has several restrictions that are not enforced by other tools.

For example, in an ECMAScript module in Node.js, any relative import must include a file extension for it to work correctly:

import * as utils from "./utils"; // Wrong
 
import * as utils from "./utils.mjs"; // Correct

Bundlers don't have this restriction, so TypeScript 5.0 introduced a new strategy, "moduleResolution": "bundler", that models how bundlers resolve imports. You can enable it in the compilerOptions section of your TypeScript configuration file:

{
    "compilerOptions": {
        "target": "esnext",
        "moduleResolution": "bundler"
    }
}

This new strategy is suitable for those using modern bundlers such as Vite, esbuild, swc, Webpack, Parcel, and others that utilize a hybrid lookup strategy.

You can check the original pull request and its implementation for more information on how moduleResolution bundler works in TypeScript.

Deprecations

TypeScript 5.0 comes with some deprecations and breaking changes, including runtime requirements, lib.d.ts changes, and API breaking changes.

  1. Runtime Requirements: TypeScript now targets ECMAScript 2018, and the package sets a minimum engine expectation of Node.js 12.20. Therefore, you need Node.js 12.20 or later to use TypeScript 5.0.
  2. lib.d.ts Changes: There have been some changes to how types for the DOM are generated, which may affect existing code. In particular, certain properties have been converted from number to numeric literal types, and properties and methods for cut, copy, and paste event handling have been moved across interfaces.
  3. API breaking changes: Some unnecessary interfaces have been removed, and some correctness improvements have been made. TypeScript 5.0 has also moved to modules.

TypeScript 5.0 has deprecated certain settings and their corresponding values, including target: ES3, out, noImplicitUseStrict, keyofStringsOnly, suppressExcessPropertyErrors, suppressImplicitAnyIndexErrors, noStrictGenericChecks, charset, importsNotUsedAsValues, and preserveValueImports, as well as prepend in project references.

In TypeScript 5.0, using any of them reports an error unless you set "ignoreDeprecations": "5.0" in your configuration, and they stopped functioning in TypeScript 5.5. If your project still uses one of them, remove it before you upgrade.

Summary

In this article, you have learned some of the major features and improvements that TypeScript 5.0 brings, such as improvements to enums, bundler resolution, and const type parameters, along with improvements to speed and size.

When your TypeScript app is ready to go live, you can deploy it with Sevalla's Application Hosting, which builds and runs it straight from your Git repository.

Deep dive into the cloud!

Deploy your application, database, or static site in minutes.