BT

TypeScript 2.2 Adds New Object Type, Better Mixin Support, and More

| by Sergio De Simone Follow 14 Followers on Feb 10, 2017. Estimated reading time: 1 minute |

Scheduled to be released sometime in February, TypeScript 2.2 has reached RC status. Besides a new JSX emit mode for React Native, it also includes a new object type to represent non-native types, better support for mixins and composable classes, and more.

The new object type provided by TypeScript 2.2 makes it possible to specify method signatures that only accept non-primitive types. This is the case, e.g., with the Object.getPrototypeOf, Object.create etc., which can syntactically accept any kind of object but will throw an exception if they are passed, e.g., a string or a number. The object type allows to catch those kind of misuses at compile-time:

  function mapObject(obj: object): object { /* ... */ }
  ...
  mapObject('string'); // type error

TypeScript 2.2 also adds more flexibility to the creation of mixins and composable types. For example, you can now create a function that takes the constructor of a Point object, declares a new class that extends Points by adding a timestamp to it, and returns a new TimestampedPoint mixin class, which can be instantiated or extended:

class Point {
  ...
}

export type Constructable = new (...args: any[]) => object;
export function Timestamped<BC extends Constructable>(Base: BC) {
    return class extends Base {
        timestamp = new Date();
    };

const TimestampedPoint = Timestamped(Point);
const p = new TimestampedPoint(10, 10);

class SpecialPoint extends Timestamped(Point) {
  z:  number;
  constructor(x: number, y: number, z: number) {
    ...
  }

}

TypeScript has long supported the use of JSX, an XML-like language extension without any defined semantics which aims to represents tree structures with attributes. A typical, although not exclusive, usage of JSX is to describe UI components. Up to version 2.1, TypeScript has been able able to handle JSX expressions in tsx files through two specific emit modes: preserve, which retains the JSX expressions so they can be consumed by a later stage, and react, which creates a js file that uses React.createElement. New in version 2.2 is a new react-native mode, which leaves all JSX expressions intact, but generates a js file, as it is required by React Native’s loader.

TypeScript 2.2rc includes more notable changes, such as:

You can try out TypeScript 2.2rc by running:

npm install -g typescript@rc

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