Was this page helpful?

TypeScript 3.1

Mapped types on tuples and arrays

In TypeScript 3.1, mapped object types[1] over tuples and arrays now produce new tuples/arrays, rather than creating a new type where members likepush(),pop(), andlength are converted.For example:

ts
typeMapToPromise<T> = { [KinkeyofT]:Promise<T[K]> };
typeCoordinate = [number,number];
typePromiseCoordinate =MapToPromise<Coordinate>;// [Promise<number>, Promise<number>]

MapToPromise takes a typeT, and when that type is a tuple likeCoordinate, only the numeric properties are converted.In[number, number], there are two numerically named properties:0 and1.When given a tuple like that,MapToPromise will create a new tuple where the0 and1 properties arePromises of the original type.So the resulting typePromiseCoordinate ends up with the type[Promise<number>, Promise<number>].

Properties declarations on functions

TypeScript 3.1 brings the ability to define properties on function declarations andconst-declared functions, simply by assigning to properties on these functions in the same scope.This allows us to write canonical JavaScript code without resorting tonamespace hacks.For example:

ts
functionreadImage(path:string,callback: (err:any,image:Image)=>void) {
// ...
}
readImage.sync = (path:string)=> {
constcontents =fs.readFileSync(path);
returndecodeImageSync(contents);
};

Here, we have a functionreadImage which reads an image in a non-blocking asynchronous way.In addition toreadImage, we’ve provided a convenience function onreadImage itself calledreadImage.sync.

While ECMAScript exports are often a better way of providing this functionality, this new support allows code written in this style to “just work” in TypeScript.Additionally, this approach for property declarations allows us to express common patterns likedefaultProps andpropTypes on React function components (formerly known as SFCs).

ts
exportconstFooComponent = ({name })=> <div>Hello!Iam {name}</div>;
FooComponent.defaultProps = {
name:"(anonymous)",
};

[1] More specifically, homomorphic mapped types like in the above form.

Version selection withtypesVersions

Feedback from our community, as well as our own experience, has shown us that leveraging the newest TypeScript features while also accommodating users on the older versions are difficult.TypeScript introduces a new feature calledtypesVersions to help accommodate these scenarios.

You can readabout it in the Publishing section of the declaration files section

The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request

Contributors to this page:
DRDaniel Rosenwasser  (51)
OTOrta Therox  (18)
CFCory Forsyth  (1)
MUMasato Urai  (1)
SSSebastian Silbermann  (1)
10+

Last updated: Nov 25, 2025