HomeTech GiantsMicrosoftTypeScript 4.9 programming language checks prerequisites with the satisfies operator

TypeScript 4.9 programming language checks prerequisites with the satisfies operator

Published on

- Advertisement -

In addition to the new operator, which specifies conditions without restricting types with additional specifications, the TypeScript 4.9 beta adjusts the in operator.

 

Microsoft has released the first beta of TypeScript 4.9. The JavaScript attachment is included in the current release satisfies a new operator to set different defaults for expressions. In addition, the in-Operator clear type assignments for the key and the object.

- Advertisement -

 

The new operator satisfies serves for different type checks in expressions. He can specify both the appropriate name and the appropriate type as a condition for properties without requiring a strict type specification in the declaration. The associated issue is titled “‘satisfies’ operator to ensure an expression matches some type”.

Among other things, you can specify that properties have the appropriate name without specifying a fixed type, as in the following example from the issue:

 

- Advertisement -
type Keys = 'a' | 'b' | 'c' | 'd';

const p = {
    a: 0,
    b: "hello",
    c: true
    // Fehler: 'd' aus 'Keys' fehlt
} satisfies Record<Keys, unknown>;

 

The default can be over Partial make it more flexible: It specifies that the names of the properties come from the condition, but do not have to exist in full. With the addition, the example above would not trigger an error, but the following would:

- Advertisement -

 

type Keys = 'a' | 'b' | 'c' | 'd';

const p = {
    a: 0,
    b: "hello",
    x: 8 // Fehler: 'x' ist nicht in 'Keys' vorhanden
} satisfies Partial<Record<Keys, unknown>>;

 

With satisfies it is also possible to vice versa, for example, that the properties may have any name, but their values ​​must correspond to a specific type. Other conditions include implementing a specific interface:

 

type Movable = {
    move(distance: number): void;
};

const car = {
    start() { },
    move(d) {
        // d: number
    },
    stop() { }
} satisfies Moveable;

 

In conjunction with the JavaScript operator in TypeScript 4.9 uses less strict type narrowing than before. The following code example from the TypeScript blog previously led to an error because TypeScript packageJSON as object has assigned that the property name does not contain:

 

interface Context {
  packageJSON: unknown;
}

function tryGetPackageName(context: Context) {
  const packageJSON = context.packageJSON;
  // Check to see if we have an object.
  if (packageJSON && typeof packageJSON === "object") {
    // Check to see if it has a string name property.
    if ("name" in packageJSON && 
        typeof packageJSON.name === "string") {
    //                     ~~~~
    // error! Property 'name' does not exist on type 'object.
      return packageJSON.name;
    //                   ~~~~
    // error! Property 'name' does not exist on type 'object.
    }
  }

  return undefined;
}

 

With the current release, TypeScript provides for the expression key in obj for existing keys, ensure that the value for key must be either a string, number, or symbol (string | number | symbol) and obj of the type object is. The above code example works without errors.

For the special value NaN, which stands for Not a Number, i.e. no numerical value, special rules apply – not only for TypeScript and JavaScript, but for all languages ​​that implement floating-point numbers according to IEEE 754. The specification states that all equality checks are to NaN false result. By definition, the following also applies:

 

console.log(NaN == NaN)  // false
console.log(NaN === NaN) // false
console.log(NaN != NaN)  // true
console.log(NaN !== NaN) // true

 

NaN is therefore also unequal NaN. That can lead to a rookie error when testing code NaN with a != performs. Therefore, TypeScript now forbids equality and inequality checks for Not a Number and suggests using the method instead Number.isNaN() before:

 

function validate(someValue: number) {
    return someValue !== NaN;
    //     ~~~~~~~~~~~~~~~~~
    // error: This condition will always return 'true'.
    //        Did you mean '!Number.isNaN(someValue)'?
}

 

Further innovations in version 4.9 can be found on the TypeScript blog. The first release candidate is scheduled to appear at the end of October and the final release is scheduled for November 15th.

- Advertisement -

Latest articles

Melhores jogos para Android e iOS da semana [08/12/2022]

Mais uma quinta-feira chegou e, como é de costume aqui no TechSmart, trazemos uma...

iPhone 14 Plus surprises in screen analysis and surpasses Galaxy S22 Ultra in DxOMark ranking

Apple is the brand with the highest presence in the DxOMark screen ranking. ...

More like this