Wednesday, March 29, 2023
Kiratas
  • Home
  • World
  • Lifestyle

    Trending Tags

    • Pandemic
  • Business
  • Entertainment
  • Sports
No Result
View All Result
  • Home
  • World
  • Lifestyle

    Trending Tags

    • Pandemic
  • Business
  • Entertainment
  • Sports
No Result
View All Result
Kiratas
No Result
View All Result
Home World

TypeScript 5.0 programming language reorganizes Decorators

Kiratas by Kiratas
March 17, 2023
in World
Reading Time: 3 mins read
0
0
SHARES
1
VIEWS
Share on FacebookShare on Twitter

TypeScript 5.0 programming language reorganizes Decorators

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.

Fresh decorators

TypeScript previously knew decorators as an experimental implementation that required the compiler flag –experimentalDecorators. 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 proposal, i.e. the standard for decorators planned for JavaScript.

Logging use case

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;
}

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

At least temporarily, TypeScript will allow both the new and the old implementation. The latter takes effect if the compiler flag –experimentalDecorators is set. Otherwise, the new decorators apply, which, unlike the old implementation, do not allow parameter decorators and cannot output the metadata via –emitDecoratorMetadata.

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

// before export is allowed: @register export default class Foo { // … } // after export is allowed: export default @register class Bar { // … } // before AND after export is not allowed: @before export @after class Bar { // … }

More accurate type inference thanks to constant parameter declaration

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

type HasNames = { names: readonly string[] };
function getNamesExactly(arg: T):
T[“names”] { … }

for TypeScript to specify the type exactly and not as a string[] recognizes. Constant only refers to the type, not the value. To declare it immutable, readonly is required.

Configuration files, enums and JSDoc

In addition, there are a few other notable innovations in the current release: from TypeScript 5.0, configuration files for projects can fall back on more than one basis under extends, and enumerations are now always union enums instead of numeric constants as before, in case of doubt.

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

More new features in TypeScript 5.0 can be found on the TypeScript blog. The programming language can be installed with npm install typescript or downloaded via NuGet.

(rme)

To home page

Tags: decoratorsECMAScriptJavaScriptLanguageMicrosoftProgrammingprogramming languagesreorganizesTypeScript

Related Posts

World

Open letter: Musk, Wozniak and Co. are demanding a forced break for models like the GPT-4

by Kiratas
March 29, 2023
World

Last minute of Pope Francis’ health: respiratory problems confirmed

by Kiratas
March 29, 2023
World

Wave of layoffs at Vodafone Germany: 900 fewer jobs

by Kiratas
March 29, 2023
World

Last minute of the Pope’s health: the Vatican confirms the Pontiff’s respiratory problems

by Kiratas
March 29, 2023
World

Remote PowerShell: Gateway to Exchange Online now with a grace period

by Kiratas
March 29, 2023
Next Post

The ten changes that the pension reform will bring

Germany ticket: Bundestag approves 49-euro ticket

Autonomous Car: Baidu is allowed to offer driverless autonomous ride-hailing in Beijing

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kiratas

Latest News from World, Health, Politics, Sports, Business, Education, Technology, Arts and Latin America, the Middle East, South Asia.
Contact Us:
[email protected]

Categories

  • Automobile
  • Business
  • Sports
  • World

Browse by Tag

Apple Artificial Intelligence Bank business ChatGPT Check Cybercrime data data protection day Energy EU euros February Google government health iOS iPhone law League Linux and Open Source live Mac Madrid March Microsoft million online photo price result Security Smartphone Software Development Spain Spanish Sánchez Test time today Vulnerabilities world year years

Recent Posts

  • Open letter: Musk, Wozniak and Co. are demanding a forced break for models like the GPT-4
  • Police identify two of the alleged attackers of the minor raped in Petrer
  • Last minute of Pope Francis’ health: respiratory problems confirmed
  • DMCA
  • Home

© Kiratas 2023. All Rights Reserved.

No Result
View All Result
  • Home
  • Landing Page
  • Buy JNews
  • Support Forum
  • Contact Us

© Kiratas 2023. All Rights Reserved.