Was this page helpful?

TypeScript 2.6

Strict function types

TypeScript 2.6 introduces a new strict checking flag,strictFunctionTypes.ThestrictFunctionTypes switch is part of thestrict family of switches, meaning that it defaults to on instrict mode.You can opt-out by setting--strictFunctionTypes false on your command line or in your tsconfig.json.

UnderstrictFunctionTypes function type parameter positions are checkedcontravariantly instead ofbivariantly.For some background on what variance means for function types check outWhat are covariance and contravariance?.

The stricter checking applies to all function types,except those originating in method or constructor declarations.Methods are excluded specifically to ensure generic classes and interfaces (such asArray<T>) continue to mostly relate covariantly.

Consider the following example in whichAnimal is the supertype ofDog andCat:

ts
declareletf1: (x:Animal)=>void;
declareletf2: (x:Dog)=>void;
declareletf3: (x:Cat)=>void;
f1 =f2;// Error with --strictFunctionTypes
f2 =f1;// Ok
f2 =f3;// Error

The first assignment is permitted in default type checking mode, but flagged as an error in strict function types mode.Intuitively, the default mode permits the assignment because it ispossibly sound, whereas strict function types mode makes it an error because it isn’tprovably sound.In either mode the third assignment is an error because it isnever sound.

Another way to describe the example is that the type(x: T) => void isbivariant (i.e. covariantor contravariant) forT in default type checking mode, butcontravariant forT in strict function types mode.

Example
ts
interfaceComparer<T> {
compare: (a:T,b:T)=>number;
}
declareletanimalComparer:Comparer<Animal>;
declareletdogComparer:Comparer<Dog>;
animalComparer =dogComparer;// Error
dogComparer =animalComparer;// Ok

The first assignment is now an error. Effectively,T is contravariant inComparer<T> because it is used only in function type parameter positions.

By the way, note that whereas some languages (e.g. C# and Scala) require variance annotations (out/in or+/-), variance emerges naturally from the actual use of a type parameter within a generic type due to TypeScript’s structural type system.

Note

UnderstrictFunctionTypes the first assignment is still permitted ifcompare was declared as a method.Effectively,T is bivariant inComparer<T> because it is used only in method parameter positions.

ts
interfaceComparer<T> {
compare(a:T,b:T):number;
}
declareletanimalComparer:Comparer<Animal>;
declareletdogComparer:Comparer<Dog>;
animalComparer =dogComparer;// Ok because of bivariance
dogComparer =animalComparer;// Ok

TypeScript 2.6 also improves type inference involving contravariant positions:

ts
functioncombine<T>(...funcs: ((x:T)=>void)[]): (x:T)=>void {
returnx=> {
for (constfoffuncs)f(x);
};
}
functionanimalFunc(x:Animal) {}
functiondogFunc(x:Dog) {}
letcombined =combine(animalFunc,dogFunc);// (x: Dog) => void

Above, all inferences forT originate in contravariant positions, and we therefore infer thebest common subtype forT.This contrasts with inferences from covariant positions, where we infer thebest common supertype.

Cache tagged template objects in modules

TypeScript 2.6 fixes the tagged string template emit to align better with the ECMAScript spec.As per theECMAScript spec, every time a template tag is evaluated, thesame template strings object (the sameTemplateStringsArray) should be passed as the first argument.Before TypeScript 2.6, the generated output was a completely new template object each time.Though the string contents are the same, this emit affects libraries that use the identity of the string for cache invalidation purposes, e.g.lit-html.

Example
ts
exportfunctionid(x:TemplateStringsArray) {
returnx;
}
exportfunctiontemplateObjectFactory() {
returnid`hello world`;
}
letresult =templateObjectFactory() ===templateObjectFactory();// true in TS 2.6

Results in the following generated code:

js
"use strict";
var__makeTemplateObject =
(this &&this.__makeTemplateObject) ||
function(cooked,raw) {
if (Object.defineProperty) {
Object.defineProperty(cooked,"raw", {value:raw });
}else {
cooked.raw =raw;
}
returncooked;
};
functionid(x) {
returnx;
}
var_a;
functiontemplateObjectFactory() {
returnid(
_a || (_a =__makeTemplateObject(["hello world"], ["hello world"]))
);
}
varresult =templateObjectFactory() ===templateObjectFactory();

Note: This change brings a new emit helper,__makeTemplateObject;if you are usingimportHelpers withtslib, an updated to version 1.8 or later.

Localized diagnostics on the command line

TypeScript 2.6 npm package ships with localized versions of diagnostic messages for 13 languages.The localized messages are available when using the--locale flag on the command line.

Example

Error messages in Russian:

sh
c:\ts>tsc --v
Version 2.6.0-dev.20171003
c:\ts>tsc --locale ru --pretty c:\test\a.ts
../test/a.ts(1,5): error TS2322: Тип""string"" не может быть назначен для типа"number".
1 var x: number ="string";
~

And help in Japanese:

sh
PS C:\ts> tsc --v
Version 2.6.0-dev.20171003
PS C:\ts> tsc --locale ja-jp
バージョン 2.6.0-dev.20171003
構文: tsc [オプション] [ファイル ...]
例: tsc hello.ts
tsc --outFile file.js file.ts
tsc @args.txt
オプション:
-h, --help このメッセージを表示します。
--all コンパイラ オプションをすべて表示します。
-v, --version コンパイラのバージョンを表示します。
--init TypeScript プロジェクトを初期化して、tsconfig.json ファイルを作成します。
-p ファイルまたはディレクトリ, --project ファイルまたはディレクトリ 構成ファイルか、'tsconfig.json' を含むフォルダーにパスが指定されたプロジェクトをコ
ンパイルします。
--pretty 色とコンテキストを使用してエラーとメッセージにスタイルを適用します (試験的)。
-w, --watch 入力ファイルを監視します。
-t バージョン, --target バージョン ECMAScript のターゲット バージョンを指定します:'ES3' (既定)、'ES5''ES2015''ES2016''ES2017''ES
NEXT'
-m 種類, --module 種類 モジュール コード生成を指定します:'none''commonjs''amd''system''umd''es2015''ESNext'
--lib コンパイルに含めるライブラリ ファイルを指定します:
'es5''es6''es2015''es7''es2016''es2017''esnext''dom''dom.iterable''webworker''scripthost''es201
5.core''es2015.collection''es2015.generator''es2015.iterable''es2015.promise''es2015.proxy''es2015.reflect''es2015.symbol''es2015.symbol.wellkno
wn''es2016.array.include''es2017.object''es2017.sharedmemory''es2017.string''es2017.intl''esnext.asynciterable'
--allowJs javascript ファイルのコンパイルを許可します。
--jsx 種類 JSX コード生成を指定します:'preserve''react-native''react'
-d, --declaration 対応する'.d.ts' ファイルを生成します。
--sourceMap 対応する'.map' ファイルを生成します。
--outFile ファイル 出力を連結して 1 つのファイルを生成します。
--outDir ディレクトリ ディレクトリへ出力構造をリダイレクトします。
--removeComments コメントを出力しないでください。
--noEmit 出力しないでください。
--strict strict 型チェックのオプションをすべて有効にします。
--noImplicitAny 暗黙的な'any' 型を含む式と宣言に関するエラーを発生させます。
--strictNullChecks 厳格な null チェックを有効にします。
--noImplicitThis 暗黙的な'any' 型を持つ'this' 式でエラーが発生します。
--alwaysStrict 厳格モードで解析してソース ファイルごとに"use strict" を生成します。
--noUnusedLocals 使用されていないローカルに関するエラーを報告します。
--noUnusedParameters 使用されていないパラメーターに関するエラーを報告します。
--noImplicitReturns 関数の一部のコード パスが値を返さない場合にエラーを報告します。
--noFallthroughCasesInSwitch switch ステートメントにcase のフォールスルーがある場合にエラーを報告します。
--types コンパイルに含む型宣言ファイル。
@<ファイル>

Suppress errors in .ts files using ’// @ts-ignore’ comments

TypeScript 2.6 supports suppressing errors in .ts files using// @ts-ignore comments placed above the offending lines.

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

A// @ts-ignore comment suppresses all errors that originate on the following line.It is recommended practice to have the remainder of the comment following@ts-ignore explain which error is being suppressed.

Please note that this comment only suppresses the error reporting, and we recommend you use this commentsvery sparingly.

Fastertsc --watch

TypeScript 2.6 brings a faster--watch implementation.The new version optimizes code generation and checking for code bases using ES modules.Changes detected in a module file will result inonly regenerating the changed module, and files that depend on it, instead of the whole project.Projects with a large number of files should reap the most benefit from this change.

The new implementation also brings performance enhancements to watching in tsserver.The watcher logic has been completely rewritten to respond faster to change events.

Write-only references now flagged as unused

TypeScript 2.6 adds revised implementation thenoUnusedLocals andnoUnusedParameterscompiler options.Declarations are only written to but never read from are now flagged as unused.

Example

Below bothn andm will be marked as unused, because their values are neverread. Previously TypeScript would only check whether their values werereferenced.

ts
functionf(n:number) {
n =0;
}
classC {
privatem:number;
constructor() {
this.m =0;
}
}

Also functions that are only called within their own bodies are considered unused.

Example
ts
functionf() {
f();// Error: 'f' is declared but its value is never read
}

The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request

Contributors to this page:
MHMohamed Hegazy  (51)
OTOrta Therox  (12)
EIEugene Ilyin  (1)
TSTAKAHASHI Shuuji  (1)
MCMert Ciflikli  (1)
5+

Last updated: Dec 16, 2025