TypeScript 3.5 release, faster, intelligent tools

TypeScript 3.5 release, this version brings some new features in the compiler, language and editing tool.

Speed ​​boost

TypeScript 3.5 introduces several optimized for the type of checks and incremental build, so that the speed increased dramatically.

type examination accelerate

TypeScript 3.4 of accidentally introduced a fallback, it could lead to an increased workload type checking is performed, and the corresponding increase in time type checking, which allows users to use the style library of components subject to greater impact.

The fallback is very serious, not only because it leads to the build time TypeScript code becomes much longer, but also makes TypeScript and JavaScript editor user operation becomes very slow.

This version focuses on optimizing some code paths and some features stripped so many times TypeScript 3.5 increments during the examination faster than TypeScript 3.3. Compared with the 3.4, not only to reduce compilation time, and automatic code completion also become faster and operate any other editor.

--incremental improvements

TypeScript 3.4 introduces a new --incremental compiler option that will save a bunch of information to .tsbuildinfo file, the file can be used to speed up subsequent calls to the tsc.

3.5 There are several versions of the compiler used to cache optimization setting state, find the cause of the file, and find the location of the files and other information. Test results show that the use of TypeScript in --build mode projects cited hundreds of scenes, compared with TypeScript 3.4, rebuild time can be reduced 68%.

Omit helper type

Many times, we want to create a target omit certain attributes, TypeScript built Pick and Exclude helper can perform similar functions. For example, if we want to define a location not attribute Person, you can write the following:

type Person = {
    name: string;
    age: number;
    location: string;
};

type RemainingKeys = Exclude<keyof Person, "location">;

type QuantumPerson = Pick<Person, RemainingKeys>;

// equivalent to
type QuantumPerson = {
    name: string;
    age: number;
};

As used herein Exclude helper type from the property Person concentrated to remove the "location" attribute, then use the Pick helper type selected from Person in the rest of the property set.

This type of operation often, users will write a Omit helper type to complete the operation:

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

TypeScript 3.5 in, lib.d.ts built a Omit type, and can be used anywhere, developers no longer need to write your own.

Everyone define their own Omit version, TypeScript 3.5 will contain its own version in lib.d.ts can be used anywhere. The compiler itself uses this Omit type to represent the type created by an object destructor declaration on the rest of generics.

Improved surplus property type of joint inspection

TypeScript has a feature called surplus property examination in a subject, this feature is designed to detect type of problem does not comply with specific attributes.

type Style = {
    alignment: string,
    color?: string
};

const s: Style = {
    alignment: "center",
    colour: "grey"
//  ^^^^^^ error! 
};

Allow certain excess property in TypeScript 3.4 and earlier versions. Below, TypeScript 3.4 name property of the object allows incorrect, even if its type does not match a Point Label.

type Point = {
    x: number;
    y: number;
};

type Label = {
    name: string;
};

const thing: Point | Label = {
    x: 0,
    y: 0,
    name: true // uh-oh!
};

Because members will not be any extra property inspector, so do not be concerned about the wrong name, but in TypeScript 3.5, and now at least a type checker verifies all the properties offered is part of a union member and have the appropriate type, this means that the above example will throw an error.

Note that, as long as the property type is valid, still allow some overlap:

const pl: Point | Label = {
    x: 0,
    y: 0,
    name: "origin" // okay
};

--allowUmdGlobalAccess flag

The new --allowUmdGlobalAccess flag, now can do from anywhere, even in a reference module UMD global declarations like the following:

export as namespace foo;

Higher Order generic type inference constructor

TypeScript 3.5 derivation operation on generic constructor integrate up:

class Box<T> {
    kind: "box";
    value: T;
    constructor(value: T) {
        this.value = value;
    }
}

class Bag<U> {
    kind: "bag";
    value: U;
    constructor(value: U) {
        this.value = value;
    }
}


function composeCtor<T, U, V>(
    F: new (x: T) => U, G: new (y: U) => V): (x: T) => V {
    
    return x => new G(new F(x))
}

let f = composeCtor(Box, Bag); // has type '<T>(x: T) => Bag<Box<T>>'
let a = f(1024); // has type 'Bag<Box<number>>'

In addition to the above combination pattern, which means that the derivation of the new generic constructor class a function operation in certain UI library components (e.g., React) can be more correctly generic class assembly operate.

type ComponentClass<P> = new (props: P) => Component<P>;
declare class Component<P> {
    props: P;
    constructor(props: P);
}

declare function myHoc<P>(C: ComponentClass<P>): ComponentClass<P>;

type NestedProps<T> = { foo: number, stuff: T };

declare class GenericComponent<T> extends Component<NestedProps<T>> {
}

// type is 'new <T>(props: NestedProps<T>) => Component<NestedProps<T>>'
const GenericComponent2 = myHoc(GenericComponent);

Smart Select

Added a function TypeScript 3.5 Smart Select, which provides an API for the editor, grammar-based approach to expand the text selection, as shown below, this function can more intelligently select the appropriate syntax structure of the code in the editor.

In addition, this feature is cross-platform, available to any server can access the correct TypeScript language editor to use.

Export type to a local type alias

These function by the Chinese developer Wang Wenlu ( @Kingwl contribution).

Complete updates check out the official blog:

https://devblogs.microsoft.com/typescript/announcing-typescript-3-5

Guess you like

Origin www.oschina.net/news/107096/typescript-3-5-released