BT

TypeScript 2.8 Release Includes Conditional Types

| by Dylan Schiemann Follow 7 Followers on Apr 05, 2018. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT