BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News TypeScript 2.6 Adds Strict Parameter Contravariance and More

TypeScript 2.6 Adds Strict Parameter Contravariance and More

This item in japanese

Among new features in TypeScript 2.6 are a new flag to enforce strict parameter contravariance and improved tooling.

TypeScript 2.6 adds a new --strictFunctionTypes compiler option to have function parameters compared strictly contravariantly.

{
"compilerOptions": {
    "strictFunctionTypes": true
}

TypeScript default behaviour is comparing function parameters bivariantly. This is required for a number of JavaScript basic features to work correctly, specifically for Array<T>. Indeed, if TypeScript used strict contravariance for parameters (e.g., a function returning a more specific type can be used wherever a function returning a more general type is expected, if the rest of the signature is the same) and strict covariance for return values (e.g., a function taking a more general type can be used wherever a function taking a more specific type is expected, if the rest of the signature is the same), then Array<T> would be invariant on T, meaning that Array<Derived> would not be assignable to Array<Animal>. Bivariant parameters, while ensuring a proper behavour of array, may give way to runtime errors, such as in the following case:


function makeLowerCase(s: string) { return s.toLowerCase(); }
declare let foo: Promise<string|number>;
foo.then(makeLowerCase); // this will fail if foo resolves to a number

The new --strictFunctionTypes option will allow to catch such kind of errors. This option is only applied to functions, not methods, to allow Arrays and other features of the language to continue to work as expected. Another useful addition to the language is the @ts-ignore comment, which allows to disable any errors occurring on the line following the comment. For example, in the following snippet the @ts-ignore comment disables the unreachable code error:


if (false) { 
    // @ts-ignore: Unreachable code error
    console.log("hello");
}

On the tooling front, the TypeScript compiler now implements a faster --watch mode, thanks to better identification of files that might be impacted by a change. Thus, only impacted files will undergo the tree transform and emit pass.

Additionally, JSDoc comments can be now transformed into proper TypeScript parameter annotations, which can make it easier to migrate a preexisting code base and have JSDoct type annotations added automatically.

TypeScript 2.6 also introduces several changes that could break existing codebases. In particular, write only references are flagged as errors when using --noUnusedLocals and --noUnusedParameters. Furthermore, in declaration files and declare module blocks, expressions are disallowed in default exports.

You can install TypeScript 2.6 by running:

npm install -g typescript

For a detailed description of what TypeScript 2.6 brings, you can read What’s new in TypeScript.

Rate this Article

Adoption
Style

BT