BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 3.5 Adds Omit Type, Smarter Union Type Checking

TypeScript 3.5 Adds Omit Type, Smarter Union Type Checking

Leia em Português

This item in japanese

The TypeScript team has announced the release of TypeScript 3.5, including type checking performance improvements and a new Omit type.

In TypeScript 3.4, the TypeScript added a new --incremental flag, which saves project graph information from the last compilation to detect the fastest way to type-check and emit changes to a project, reducing subsequent compile times. TypeScript 3.5 improves upon this approach, as explained by TypeScript program manager Daniel Rosenwasser:

TypeScript 3.5 includes several optimizations to caching how the state of the world was calculated – compiler settings, why files were looked up, where files were found, etc. In scenarios involving hundreds of projects using TypeScript’s project references in --build mode, we’ve found that the amount of time rebuilding can be reduced by as much as 68% compared to TypeScript 3.4!

To speed up type checking time in general and to address some type checking performance regressions introduced in 3.4, the TypeScript team focused on optimizing code paths and functionality, such that TypeScript 3.5 is faster than TypeScript 3.3 for many incremental type checks, making compiler time, code completion, and other editor operations faster as well.

A typical TypeScript pattern creates a new object that omits specific properties from another object. Before TypeScript 3.5, developers would commonly use a combination of the Pick and Exclude helpers to define an omit pattern. The TypeScript 3.5 release adds a new Omit type. An example provided by the TypeScript team shows the process of defining a Person with no location property before and after the TypeScript 3.5 release.

TypeScript 3.4 without Omit:

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;
};

TypeScript 3.5 with Omit:

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

Developers can leverage Omit and other builtin helper types via lib.d.ts.

TypeScript supports excess property checking in object literals, which detects typos when a type contains an unexpected property. Before TypeScript 3.5, some excess properties were permitted, such as a name property on an object literal because a non-discriminated union would not perform excess property checking on its members. TypeScript 3.5 verifies all provided properties belong to some union member and the appropriate type.

TypeScript 3.5 improves union type checking. When checking against union types, TypeScript typically compares each constituent type in isolation. Before TypeScript 3.5, in some cases, the type checking would fail when a type definition was not sufficiently specific. For example, if one member allowed a value of true or false, and another member allowed the same property to be a boolean, this would fail. In TypeScript 3.5, the language now decomposes types into a union of all possible types. Because boolean is a union of true and false, this example would now succeed in its type check.

TypeScript 3.4 introduced support for functions to infer types from other generic functions. TypeScript 3.5 generalizes this to also work on constructor functions. This new inference on generic constructors allows functions operating on class components in some UI libraries like React to more correctly operate on generic class components.

Other new features in TypeScript 3.5 include the --allowUmdGlobalAccess flag, a Smart Select API for editors to expand text selections outward in a syntactically aware manner, and refactoring to extract types to local type aliases.

TypeScript 3.5 introduces potentially breaking changes, all of which are consequences of using the new features:

  • Generic type parameters are implicitly constrained to unknown
  • { [k: string]: unknown } is no longer a wildcard assignment target
  • Fixes to unsound writes to indexed access types
  • Object.keys rejects primitives in ES5
  • lib.d.ts includes the Omit helper type

The TypeScript team is already working on features for TypeScript 3.6, including improvements to generators and iterators, support for ECMAScript private class fields, and more compiler, infrastructure, and editor improvements.

The TypeScript community is also preparing for the second TSConf event on October 11th with TypeScript founder Anders Hejlsberg delivering the keynote.

TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project and should follow the TypeScript contribution guidelines and Microsoft open source code of conduct.

Rate this Article

Adoption
Style

BT