BT

Your opinion matters! Please fill in the InfoQ Survey!

TypeScript 2.6 Adds Strict Parameter Contravariance and More

| by Sergio De Simone Follow 5 Followers on Nov 01, 2017. Estimated reading time: 2 minutes |

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 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