BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 1.8 Brings Module Augmentation Support

TypeScript 1.8 Brings Module Augmentation Support

This item in japanese

Microsoft has released version 1.8 of TypeScript, its open source superset of JavaScript.

Following on from the language's beta announcement last month, Microsoft program manager Bowden Kelly wrote in the blog post Announcing TypeScript 1.8 that on top of bringing the ability to use JavaScript code within a TypeScript project, the latest version also "provides full support for module augmentation."

Elaborating on the subject, Kelly said that would "allow users to design more modular libraries via module augmentation," helping them to extend existing modules and letting them choose if they want to import the whole module or a subset of it. This is achieved by adding an ambient module declaration and extending existing types.

Kelly provides the following example:

// scale.ts
export class Scale {
    weightOnEarth(mass) {}
}
// advancedScale.ts
import { Scale } from "./scale" ;
// create augmentation for Scale
declare module "./scale" {
    // Augment Core class via interface merging
    interface Scale {
        weightOnMoon(mass); // not everyone needs moon weight
    }
}
Scale.prototype.advancedMethod = /* insert implementation */;

// consumer.ts
import { Scale } from "./scale";
import "./advancedScale";

let scale: Scale;
scale.weightOnMoon(10);  // ok

Microsoft first started supporting React with the beta release of version 1.6 last September, and version 1.8's stable release has simplified the declaration of props types in React.

Succinctly explained, developers no longer need to explicitly declare ref and key or extend React.Props. On top of this, the update means ref and key properties will now appear with correct types on all components, and ref is correctly disallowed on instances of Stateless Function components.

Improved in TypeSccript 1.8 is the language's union/intersection type inference, such as when inferring from string | string[] to string | T, the types are reduced to string[] and T, inferring string[] for T, as in the below example

type Maybe<T> = T | void;

function isDefined<T>(x: Maybe<T>): x is T {
    return x !== undefined && x !== null;
}

function isUndefined<T>(x: Maybe<T>): x is void {
    return x === undefined || x === null;
}

function getOrElse<T>(x: Maybe<T>, defaultValue: T): T {
    return isDefined(x) ? x : defaultValue;
}

function test1(x: Maybe<string>) {
    let x1 = getOrElse(x, "Undefined");         // string
    let x2 = isDefined(x) ? x : "Undefined";    // string
    let x3 = isUndefined(x) ? "Undefined" : x;  // string
}

function test2(x: Maybe<number>) {
    let x1 = getOrElse(x, -1);         // number
    let x2 = isDefined(x) ? x : -1;    // number
    let x3 = isUndefined(x) ? -1 : x;  // number
}

Full details of improvements and changes in TypeScript 1.8 can be found on the project's GitHub here.

Rate this Article

Adoption
Style

BT