Nintendo-Power

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

Programming Language: TypeScript 4.5 Tricks with Node.js modules

Programming Language: TypeScript 4.5 Tricks with Node.js modules

Microsoft has released the first beta of TypeScript 4.5. The release brings changes to the interaction with ECMAScript modules in Node.js. In addition, it eliminates final cycle function calls in some cases and takes two planned innovations in ECMAScript quality.

In addition to the commonly used Common JS (CJS) module structure, Node.js can now process ECMAScript modules (ESM) as well. For communication, TypeScript 4.5 leads under compilerOptions The module-Settings node12 And nodenext A

New module methods test whether a .ts-, .tsx-., .Js-exists. Or .jsx file is an ES module. In this case, the system imports more modules according to the guidelines for ESM. Among other things, the associated import path must have a file name extension. For example, the specification import "./foo.js" While in CJS import "./foo") Allowed without result.

Based on the type of module can be found in the Node.js on the one hand package.json entry "type": "module" Or "type": "commonjs" Declare ESM or CJS module. Additionally, Node.js allows explicit definition of file extensions via .mjs for EMS and .cjs for CJS. Similarly, TypeScript 4.5 introduces extensions .mts and .cts.

The two changes are about updates to the typescript under Hood. In this way, the system tries to recognize whether a recursion will run indefinitely or whether a type of expansion will take too long. Some types may mistype the heuristics used for typescript detection. For example, a blog post lists a portion of the code that removes leading spaces from categories such as:

See also  His plays "Diablo 2 resurrected": What good is a remaster?

type TrimLeft<T extends string> =
    T extends ` ${infer Rest}` ? TrimLeft<Rest> : T;

// Test = "hello" | "world"
type Test = TrimLeft<"   hello" | " world">;

The implementation works for the sample code. However, if a character string contains too many spaces at the beginning, heuristic detection leads to an error message

error: Type instantiation is excessively deep
and possibly infinite.

However, in particular use, overhead is less critical because the function on a branch is repeated and ends directly without processing it. TypeScript 4.5 recently deletes terminal function calls when a branch of the condition type is again a condition type. This way, there are no additional immediate requirements, which will infuse the memory requirements of the repetitive process into infinity.

Typescript 4.5 introduces a new parameter --preserveValueImports Prevents automatic removal of apparently unused imports. For example, a blog post uses code support eval Return:

import { Animal } from "./animal.js";

eval("console.log(new Animal().isDangerous())");

Although used eval Typescript and JavaScript are questionable for security reasons, for example proving a case in which Typescript can be mistaken for an import redundancy and delete it because the JavaScript module is used within the text string. This also applies to interactions with structures such as Vue.js or Svelte. There, some imports are not immediately recognizable as they are used for the configuration system because they are inside script-Paragraphs are involved.

With two innovations, Typescript takes ECMAScript recommendations, which are not yet standard, but planned. One, you can check if the typescript contains a private field defined in the current class in an object:

See also  19th edition of Rendez-vous in gardens in New Aquitaine

class Person {
    #name: string;
    constructor(name: string) {
        this.#name = name;
    }

    equals(other: unknown) {
        return other &&
            typeof other === "object" &&
            #name in other && // <- this is new!
            this.#name === other.#name;
    }
}

That Related ECMA Project Is entitled “Ergonomic brand checks for the private sector” (roughly translated as “Ergonomic brand checks for the private sector”). The private sector can be used to test whether this is really an example of class because they are only in this case.

In addition,. Can also be used for ECMAScript Import assurances Use in Typescript 4.5. They make sure the imported file is in a specific format.

import obj from "./something.json" assert { type: "json" };

However, the configuration system does not observe actual verification of the design of the content because the responsibility for this rests with the browser or runtime.

More innovations can be found in Typescript 4.5 From the Development Blog at Microsoft. The beta phase of Typescript 4.5 is scheduled for a good six weeks, and final release According to the map Released on November 16, 2021.


(rme)

To the home page