BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 2.8 Release Includes Conditional Types

TypeScript 2.8 Release Includes Conditional Types

This item in japanese

Bookmarks

TypeScript 2.8 has been released with several major features and bug fixes. The most significant addition is conditional types, a new construct that allows engineers to choose types based on other types.

Conditional types are most useful in combination with generics. If a framework has to repeat the same choice throughout its API, it becomes a cumbersome process. Similar to how JavaScript makes decisions at runtime based on the characteristics of a value, conditional types allow TypeScript to make decisions in the type system based on the characteristics of other types.

An example provided by the TypeScript team shows replacing the following more tedious API:

interface Id { id: number, /* other fields */ }
interface Name { name: string, /* other fields */ }

declare function createLabel(id: number): Id;
declare function createLabel(name: string): Name;
declare function createLabel(name: string | number): Id | Name;

With a more flexible interface:

type IdOrName<T extends number | string> =
    T extends number ? Id : Name;

declare function createLabel<T extends number | string>(idOrName: T):
    T extends number ? Id : Name;

Conditional types add support for inferring from types using the infer keyword instead of manually fetching the type. An example provided by the TypeScript team creates a type, Flatten, that flattens array types to their element types:

type Flatten<T> = T extends any[] ? T[number] : T;

With infer, this can be simplified to:

type Flatten<T> = T extends Array<infer U> ? U : T;

Conditional types also improve union types, by making it possible to distribute on union types with conditional types.

Additionally, TypeScript adds a number of new type aliases (Exclude, Extract, InstanceType, NonNullable, ReturnType) that leverage conditional types via lib.d.ts. Therefore conditional types have made it straightforward for the TypeScript team to add additional common typing patterns to the language more efficiently.

The TypeScript 2.8 release also adds an --emitDeclarationOnly flag for decoupling the creation of TypeScript definition files from JavaScript files, refining the process for using Babel 7 to transpile TypeScript code. React and JSX users receive a few additional refinements around pragma comments and JSX resolution. 

Mapped type modifiers can now also remove modifiers using the "-" operator, or via a new Required type that removes optionality from each property. Other smaller enhancements include utilities for organizing ES module imports and improvements to uninitialized class properties checks.

Overall, the TypeScript 2.8 release has several improvements, most of which build upon the introduction of conditional types, improving the approach for expressing more complex type definitions.

TypeScript 2.8 is available via npm with the npm install -g typescript command, or via GitHub. Efforts on TypeScript 2.9 are already underway, with a release anticipated in late May.

Rate this Article

Adoption
Style

BT