Nintendo-Power

Daily Gaming news, videos, reviews, tips & guides. Let's share our love of BigN games!

Programming Language: TypeScript 4.4 allows codes to be code signatures

Programming Language: TypeScript 4.4 allows codes to be code signatures

Microsoft has released the first beta of TypeScript 4.4. The release brings minor changes to the language level and new options for using the genre unknown In dealing with try-like modules and custom attributes.

Typescript uses default flow analysis to determine the correct type of variables. In the following code example from the Typescript blog, the compiler uses what is called a typecard concept arg Within that if-Blocks Ten Type string To be compelled.

function foo(arg: unknown) {
    if (typeof arg === "string") {
        // We know this is a string now.
        console.log(arg.toUpperCase());
    }
}

So far, the depth of the typecard code has not been explored. Therefore, a type test using a constant led to the error message: “Error! The property ‘toUpperCase’ type is not in ‘unknown’, although here the program flow is inside if-Only one ban arg Type string Reached:

function foo(arg: unknown) {
    const argIsString = typeof arg === "string";
    if (argIsString) {
        console.log(arg.toUpperCase());
    }
}

TypeScript 4.4 expands the control flow analysis for TypeCard, meaning construction is now allowed. The analysis works equally well const And readonly Properties and unchanged parameters. In addition to reviews about typeof The testing of members for individual union elements also operates.

The second linguistic addition is about access to elements via code signatures Map– Map of similar configurations via an interface:

interface BooleanDictionary {
    [key: string]: boolean;
}

declare let myDict: BooleanDictionary;

// Valid to assign boolean values
myDict["foo"] = true;
myDict["bar"] = false;

Previously, the main type of access was via code signatures string And number Limited. Version 4.4 of the programming language extends the spectrum symbol-Keys:

interface Colors {
    [sym: symbol]: number;
}

const red = Symbol("red");
const green = Symbol("green");
const blue = Symbol("blue");

Likewise, access can no longer be restricted to cleanliness stringElements, but also via such string patterns `color-${string}` Proceed.

There are also some minor innovations that can be implemented via options. So the parameter does --useUnknownInCatchVariables This means that the variables in the try-capture modules are not the default any, But as unknown Are reported. TypeScript 4.0, released almost a year ago, already allowed explicit specification any Or unknown For that catch-Path.

When checking the code with the flag --strict The new parameter is set automatically, which can lead to incompatibility.

Another adjustment is about custom attributes: parameter --exactOptionalPropertyTypes Ensures that the compiler allows only the explicitly specified type, not the alternative, as before undefined. For the prototype interface

interface Person {
    name: string,
    age?: number;
}

Up to TypeScript 4.3, the following types of explicit work are allowed:

const p: Person = {
    name: "Daniel",
    age: undefined,
};

Domestically, the property was age?: number | undefined; Are defined. The current version of the programming language, however, reacts with the error message number Direct work is allowed.

Other innovations, such as the one modified in Typescript 4.4 --help-Can be optional Microsoft Development Blog Remove. The beta phase of Typescript 4.4 is scheduled for two months, with a final release The road should be according to the map It is set to be released on August 24th.


(rme)

Home page