Programming language: TypeScript 4.2 flexibly distributes the tuple remainder

Source: Heise.de added 14th Jan 2021

  • programming-language:-typescript-4.2-flexibly-distributes-the-tuple-remainder

Microsoft has released the first beta of TypeScript 4.2. The most obvious change concerns tuples, which can now have remainder elements not only at the end, but also at the beginning or in the middle. In addition, the programming language maintains type aliases more consistently and performs more stringent checks on the in operator.

The rest in the middle TypeScript allows for Tuple remainder elements for a long time, which, like remainder parameters in JavaScript functions, represent any number of elements:

let rest ramp: ]; // The tuple always contains exactly two strings // in addition, any number of numbers may be at the end resterampe = [“keine”, “Zahlen”]; rest ramp = [“eine”, “Zahl”, 1]; rest ramp = [“viele”, “Zahlen”, 1, 2, 3, 4]; Previously, the remainder element, like the remainder parameter in JavaScript, could only be at the end. TypeScript 4.2 now also allows its use at the beginning and in the middle of the element list. However, only one remainder element is allowed and no further optional element may follow after it:

// a single remainder element is allowed in any position let allowedA: […string[], boolean]; let allowedB: [number, …string[], boolean]; // two remainder elements are not permitted let forbiddenA: […string[], … boolean []]; // the remainder element must not be followed by an optional let forbiddenB: […string[], boolean?]; Parameter list with elements Even if remainder parameters are still not allowed in functions at the beginning or end, a single tuple as a parameter can achieve the same goal:

declare function flexible (… args: [ … texts: string [], number: number]): void; flexible (0); flexible (“do”, 1); flexible (“do”, “re”, “mi”, 3); Alias ​​remains alias Another new feature concerns type aliases, which TypeScript now keeps better under the hood. The adaptation is particularly noticeable in interaction with development environments and editors such as Viusal Studio Code. When hovering the mouse over them, they usually show the alias instead of the specific type. However, with union types in conjunction with undefined the original types are shown. TypeScript 4.2 now consistently retains the alias.

In addition, the current release tries to handle incorrectly while writing code to remind the operator in , which is only allowed for object types and not for primitives.

Abstract construct: abstract constructor In addition, abstract constructors are now allowed which, like abstract classes, use the keyword abstract receive. This indicates that the constructor should not be called directly. Non-abstract constructor types can be assigned to the abstract ones, but the reverse is not allowed.

As for abstract classes, that instantiating via new is not permitted for elements with abstract constructors. The innovation aims, among other things, to implement mixins with abstract classes, as the following example from the associated pull request shows:

interface Mixin {…} abstract class Base {…} declare function Mixin1 {}> (base: TBase): TBase & (new (… args: any []) => Mixin); class Sub1 extends Mixin1 (Base) {} // ^^^^ TS 2345: // Argument of type ‘typeof Base’ is not assignable // to parameter of type ‘new (… args: any []) => {}’. // Cannot assign an abstract constructor type to a // non-abstract constructor type. declare function Mixin2 {}> (base: TBase): TBase & (new (… args: any []) => Mixin); class Sub2 extends Mixin2 (Base) {} // Ok Further innovations in TypeScript 4.2 such as the implementation of template literal expressions using the template introduced in version 4.1 Literal types can be found on the TypeScript blog. The beta phase for TypeScript 4.2 is planned for a good month. The release candidate should appear on February 9th, and for the 23. February is the finished release on the plan.

(rme)

Read the full article at Heise.de

brands: CODE  Element  First  MERCURY  Microsoft  New  other  RME  Writing  
media: Heise.de  
keywords: Internet  

Related posts


Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Related Products



Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91