Mobile Monitoring Solutions

Search
Close this search box.

TypeScript 3.1 Adds Mappable Tuple and Array Types

MMS Founder
MMS RSS

Article originally posted on InfoQ. Visit InfoQ

The TypeScript team recently announced version 3.1 of TypeScript, adding mappable tuple and array types and several other refinements on the heels of the 3.0 release.

Nearly every JavaScript application needs to map over values in a list, a pattern which gets simplified with ES2015 via rest parameters.

A common example provided by the TypeScript team:

function stringifyAll(...elements) {
    return elements.map(x => String(x));
}

Per the TypeScript release blog:

The stringifyAll function takes any number of values, converts each element to a string, places each result in a new array, and returns that array. If we want to have the most general type for stringifyAll, we’d declare it as so:

declare function stringifyAll(...elements: unknown[]): Array<string>;

With this example, the function accepts an arbitrary number of elements and will return an array of strings, but type information about the number of elements would be lost. Previously the solution would be to overload function definitions, which quickly becomes inconvenient:

declare function stringifyAll(...elements: []): string[];
declare function stringifyAll(...elements: [unknown]): [string];
// ... etc.

TypeScript already introduced a mapped object type in a previous release, but this did not work as expected with tuple and array types. The change with this release is that this approach now works as expected rather than throwing cryptic error messages.

Per the TypeScript release blog:

While technically consistent in behavior, the majority of our team felt that this use-case should just work. Rather than introduce a new concept for mapping over a tuple, mapped object types now just “do the right thing” when iterating over tuples and arrays. This means that if you’re already using existing mapped types like Partial or Required from lib.d.ts, they automatically work on tuples and arrays now. While very general, you might note that this functionality means that TypeScript is now better-equipped to express functions similar to Promise.all. While that specific change hasn’t made its way into this release, it may be coming in the near future.

The other significant addition to the 3.1 release is ease of specifying properties on function declarations. React users are familiar with this as defaultProps.

As functions are just objects in JavaScript, it is easy to add properties to a function. TypeScript’s original solution to this problem was namespaces, but this has introduced challenges when working with ES Modules, and do not merge with declarations made via var, let, or const

For TypeScript 3.1, any function declaration or const declaration initialized with a function results in the type-checker analyzing the containing scope to track properties that get added.

Many other smaller changes and enhancements made the 3.1 release.

Beyond the 3.1 release, there are substantial improvements due in the TypeScript 3.2 release. The largest is Strict bind, call, and apply methods on functions, a complex enhancement the community requested nearly four years ago. This fix is yet another piece of the missing puzzle for variadic kinds, the most challenging collection of problems to solve to support the typing of higher-order functions. BigInt support is also part of the 3.2 release.

TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project.

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.