Microsoft released TypeScript 5.0 on schedule almost two months after the first beta. The stable release brings a fresh concept for decorators that builds on plans for ECMAScript. In addition, parameters can be declared as constants to improve type inference.

TypeScript 5.0 is not a fresh major version: the team uses decimal counting for the programming language and increases the decimal place by one for each feature release. Consequently, TypeScript 5.0 follows version 4.9, just like 2020 TypeScript 4.0 follows version 3.9.

TypeScript previously knew decorators as an experimental implementation that used the compiler flag --experimentalDecorators required. Decorators are functions that are called on classes or their functions and properties. You can replace, initialize, or provide advanced access to elements. The current release turns the integration inside out and relies on the current status of the associated ECMAScript proposalsi.e. the standard for decorators planned for JavaScript.

A typical example, which can also be found in the ECMAScript proposal, is replacing a method with a general method that outputs a message to the console each time it is called in order to track down errors. An example of this can be found on the TypeScript blog. The following code shows the simple variant without TypeChecking:

function loggedMethod(originalMethod: any, _context: any) {

    function replacementMethod(this: any, ...args: any()) {
        console.log("LOG: Entering method.")
        const result = originalMethod.call(this, ...args);
        console.log("LOG: Exiting method.")
        return result;
    }

    return replacementMethod;
}

one with @loggedMethod decorated method uses this function as a replacement. It first gives “LOG: Entering method.” then executes the original method (originalMethod.call) to finally “LOG: Exiting method.” to write to the console.

At least temporarily, TypeScript will allow both the new and the old implementation. The latter intervenes when the compiler flag --experimentalDecorators is set. Otherwise, the new decorators apply, although they are different from the old implementation no parameter decorators allowed and the metadata does not have --emitDecoratorMetadata can spend.

Compared to the beta, the team has added a little something to the decorators: A decorator can either be in front of or behind, as in the ECMAScript proposal export stand. Both positions are allowed, but must not appear mixed:

//  vor export ist erlaubt:
@register export default class Foo {
    // ...
}

//  hinter export ist erlaubt:
export default @register class Bar {
    // ...
}

//  vor UND hinter export ist nicht erlaubt:
@before export @after class Bar {
    // ...
}

Another addition in TypeScript 5.0 is intended to improve type inference: parameters can be declared as constants. So care the keyword const in

type HasNames = { names: readonly string() };
function getNamesExactly<const T extends HasNames>(arg: T): 
  T("names") { ... }

for TypeScript to use the type exactly and not as string() recognizes. Constant only refers to the type, not the value. To declare it immutable is readonly necessary.

In addition, there are a few other notable innovations in the current release: From TypeScript 5.0, configuration files for projects may be used on more than one basis extends and enumerations are now always union enums instead of numeric constants as before.

In interaction with JSDoc, TypeScript 5.0 knows two new distinctions: @overload for overloaded functions and @satisfies for the operator introduced in TypeScript 4.9 satisfies.

Other innovations in TypeScript 5.0 can be found on the TypeScript blog. The programming language can be used with npm install typescript install or Download via NuGet.


(rme)

To home page

California18

Welcome to California18, your number one source for Breaking News from the World. We’re dedicated to giving you the very best of News.

Leave a Reply