TypeScript 3.1

TypeScript is an free, open-source and cross-platform language that builds on top of modern JavaScript. To know more about TypeScript you can refer their official documentation.  You can able to install the latest version of TypeScript via npm like,

npm install -g typescript@latest

The following editor supports typescript:

  • Visual Studio 2017
  • Visual Studio 2015
  • Visual Studio code
  • Sublime

What’s new in TypeScript 3.1?

Let us see what are all the features available in TypeScript 3.1.

  • Mapped types on tuples and arrays
  • Properties declarations on functions
  • Version selection with typesVersions

Mapped types on tuples and arrays

In TypeScript 3.1, mapped object types over tuples and arrays now produce new tuples/arrays, rather than creating a new type.

type MapToPromise<T> = { [K in keyof T]: Promise<T[K]> };

type Coordinate = [number, number]

type PromiseCoordinate = MapToPromise<Coordinate>; // [Promise<number>, Promise<number>]
  • In the above example, MapToPromise takes a type T, and when that type is a tuple like Coordinate, only the numeric properties are converted.
  • In [number,number], there are two numerically named properties: 0 and 1. When given a tuple like that,  MapToPromise will create a new tuple where the 0 and 1 properties are Promise’s of the original type.
  • So the resulting type PromiseCoordinate ends up with the type [Promise<number>, Promise<number>].

Properties declarations on functions

In TypeScript 3.1 you can able to define the properties on function declarations and const-declared functions.

For any function declaration or const declaration that’s initialized with a function, the type-checker will analyze the containing scope to track any added properties.

Version selection with typesVersions

In TypeScript 3.1, it opens the package.json file to figure out which files it needs to read. In this file it looks for a new field called typesVersions.

{
    "name" : "package-name",

    "version" : "1.0",

    "types" : "./index.d.ts",

    "typesVersions" : {

        ">=3.1" : {"*" : ["ts3.1/*"]}

     }

}

The package.json files checks whether the current version of typescript is running or not.  The condition >=3.1 : {“*” : [“ts3.1/*”]} checks the tyepscript version and if it 3.1 or later it reads the package from ts 3.1’s folder. If the typescript version is less than 3.1 it reads the package from index.d.ts’s folder.

typesVersions also support multiple fields where we can specify like which version of typeScript should choose the respective folder. It can be specified like,

{

     "name" : "package-name",

     "version" : "1.0",

     "types" : "./index.d.ts",

     "typesVersions" : {

           ">=3.2" : {"*" : ["ts3.2/*"]},

           ">=3.1" : {"*" : ["ts3.1/*"]}
     }
}

These are the features that are available in TypeScript 3.1. You can take a look tof typescript roadmap to get an idea of upcoming features.

Stay tuned for more blogs.

Happy Coding!

Cheers 🙂

One thought on “TypeScript 3.1

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: