TypeScript 3.8 adds support for ECMAScript private fields and top-level await

Share
  • February 21, 2020

Microsoft has released TypeScript 3.8, as Daniel Rosenwasser, Program Manager of TypeScript, announced in a blog post. TypeScript is a popular superset of JavaScript that adds syntax for types which can be analyzed through static type-checking before running the code.

SEE ALSO: Angular 9 – The Future of Angular with Ivy

The new release finalizes the features you may have tried out in the beta version that landed last month.

Top-level await

TypeScript users on Twitter are especially excited about the new feature top-level await:

Top-level await is an upcoming ECMAScript feature that allows await to be used at the top level of a module. Previously, this was only possible within the body of an async function.

The new feature can be used as seen here:

const response = await fetch("...");
const greeting = await response.text();
console.log(greeting);

// Make sure we're a module
export {};
import type { SomeThing } from "./some-module.js";

export type { SomeThing };

The new command import type imports declarations only to be used for type annotations and declarations, whereas the export provided by export type can be used only for type contexts.

Both are always fully erased from the TypeScript output and leave no remnant at runtime.

ECMAScript private fields

Support for ECMAScript private fields has been added as well.

The ECMAScript proposal is currently under development in stage 3. Private fields start with #, do not permit TypeScript accessiblity modifiers like public or private, and cannot be accessed or detected outside of the containing class, which is referred to as “hard privacy.”

Here’s how to use private fields, as shown in the blog post:

class Person {
    #name: string

    constructor(name: string) {
        this.#name = name;
    }

    greet() {
        console.log(`Hello, my name is ${this.#name}!`);
    }
}

let jeremy = new Person("Jeremy Bearimy");

jeremy.#name
//     ~~~~~
// Property '#name' is not accessible outside class 'Person'
// because it has a private identifier.

Breaking changes

Among other features, some breaking changes have made it into TypeScript 3.8–but according to Daniel Rosenwasser, they are minor breaking changes. For example, unions with index signatures have received stricter assignability checks. The use of Object and object has been clarified, in that lowercase object now “really refers to the non-primitive object type.”

SEE ALSO: JavaScript developers average 2020 salaries hits $114,986 in the US

TypeScript 3.8 can be installed as NuGet package, via the npm command npm install typescript or in the Marketplace for Visual Studio 2017 or 2019.

See the blog post by Daniel Rosenwasser for further details.

The post TypeScript 3.8 adds support for ECMAScript private fields and top-level await appeared first on JAXenter.

Source : JAXenter