TypeScript 5.6 no longer allows some code

0
27
TypeScript 5.6 no longer allows some code


The new programming language version TypeScript 5.6 has been released. Microsoft has added some new features to the JavaScript superset, but also reversed a change from the beta stage that affected the discovery of the tsconfig.json file. New are strict null and true checks, iterator helper methods, and strict, unified iterator checks.

Advertisement


The beta version released at the end of July introduced changes to the TypeScript language service Searched for tsconfig.json files. The language service was able to continue searching each possible project file named tsconfig.json instead of stopping at the first file as before. However, this could lead to multiple referenced projects. The development team therefore initially decided to reverse this behavior and possibly reintroduce it in a modified form in version 5.7.

TypeScript 5.6 introduced a change that will not allow certain expressions with strict null and truth checks. As the TypeScript team explains using some examples, this may be syntactically valid JavaScript code, but it doesn’t do what the developer had in mind. TypeScript used to accept these cases, but now the compiler is giving an error.

If an expression is always true or never null then these error messages result:

if (/0x(0-9a-f)/) {
//  ~~~~~~~~~~~~
// error: This kind of expression is always truthy.
}

if (x => 0) {
//  ~~~~~~
// error: This kind of expression is always truthy.
}

function isValid(value: string | number, options: any, strictness: "strict" | "loose") {
    if (strictness === "loose") {
        value = +value
    }
    return value < options.max ?? 100;
    //     ~~~~~~~~~~~~~~~~~~~
    // error: Right operand of ?? is unreachable because the left operand is never nullish.
}

if (
    isValid(primaryValue, "strict") || isValid(secondaryValue, "strict") ||
    isValid(primaryValue, "loose" || isValid(secondaryValue, "loose"))
) {
    //                    ~~~~~~~
    // error: This kind of expression is always truthy.
}

As the TypeScript team points out, the same results can be achieved with the rule no-constant-binary-expression in the code analysis tool ESLint. However, this is not a complete overlap, so the TypeScript change creates added value.

More security and flexibility with open source in automobile manufacturingMore security and flexibility with open source in automobile manufacturing

Some expressions are still allowed true, false, 0 And 1which are always true or null, as in the following example:

while (true) {
    doStuff();

    if (something()) {
        break;
    }

    doOtherStuff();
}

Other language features added include a new type called IteratorObject and the new compiler option --noCheck To skip type checking for all input files.

Iteration scheme for TypeScript 5.7 Already available on GitHub to track prioritized features, bug fixes and planned release dates. Accordingly, the next version is scheduled to appear on November 21, 2024.

Information about all other innovations in the current release can be found at Microsoft’s TypeScript blog,


(May)

Apple presents the AirPods 4 that will be available on September 20Apple presents the AirPods 4 that will be available on September 20

LEAVE A REPLY

Please enter your comment!
Please enter your name here