LoginSignup
89
84

More than 5 years have passed since last update.

TypeScript2系のコンパイラのオプション一覧

Posted at

TypeScriptのコンパイルコマンド、tscの--helpで確認できるコマンドをまとめてみました。
バージョンは2.1.5です。
1.6.2の記事もあります。

$ tsc --version
Version 2.1.5

コメント、編集リクエストお待ちしております。

--allowJs

Allow javascript files to be compiled.
コンパイルする対象にJSを含めてもエラーを発生させないことができます。
コンパイル対象にJSを指定しているときにこのオプションを有効にしていない場合、次のエラーが発生します。

error TS6054: File '[JSファイル]' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.

--allowSyntheticDefaultImports

Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
次のようにexport defaultを使用していないモジュールのコンパイル時にエラーを出力しないようにすることができます。

module.ts
function module() {
    return true;
}

export = module;
main.ts
import module from './modules/module';
var m = module();

このオプションを有効化していないと次のようなエラーが発生します。

error TS1192: Module '[モジュールのパス]' has no default export.

--allowUnreachableCode

Do not report errors on unreachable code.
到達不能コードのエラーを抑制することができます。
TypeScriptは次のような到達不能コードがあるとエラーを発生します。

if (true) {
    alert('true');
} else {
    alert('false'); // 到達不能
}
error TS7027: Unreachable code detected.

このオプションを有効にするとこのエラーを発生させないことができます。

--allowUnusedLabels

Do not report errors on unused labels.
未使用のラベルがあるときにエラーを抑制することができます。
公式の下記のコードの通り標準ではエラーが発生します。

l: // error will be reported - label `l` is unused
while (true) {
}

(x) => { x:x } // error will be reported - label `x` is unused
error TS7028: Unused label.

これを抑制することができます。

--alwaysStrict

Parse in strict mode and emit "use strict" for each source file
各出力ファイルにuse strictの記述を出力します。

--baseUrl URL

Base directory to resolve non-absolute module names.
モジュールを読み込むときの基底のパスを指定することができます。
例えば、src/modules/TestModuleとあった時にbaseUrlをsrc/modulesとしておけばわざわざ前のsrc/modulesを付けることなくTestModuleという名前だけで読み込むことができます。

-d, --declaration

Generates corresponding '.d.ts' file.
対応する.d.tsファイルを生成します。
TypeScriptで記述したファイルから型定義ファイルを生成します

--experimentalDecorators

Enables experimental support for ES7 decorators.
ES7から実装されるdecorators(仕様未確定?)のサポートを有効化

--forceConsistentCasingInFileNames

Disallow inconsistently-cased references to the same file.
大文字小文字を区別して参照を解決するようにします。
Windowsだとファイルのパスは大文字小文字を区別しません。
なのでモジュールの名前の解決のときに、Windowsではエラーが出ないのにLinuxではエラーが発生するという差異が生じます。
Windowsのような大文字小文字を区別しない環境でも同じようにエラーを発生させることができるのがこのオプションです。
ちなみに大文字小文字のエラーは次のようなものが出ます。

error TS1149: File name '[パス]/person.ts' differs from already included file name '[パス]/Person.ts' only in casing

-h, --help

Print this message.
ヘルプを表示します。

--importHelpers

Import emit helpers from 'tslib'.
tslibをヘルパーとして使用するように設定できます。
参考

--init

Initializes a TypeScript project and creates a tsconfig.json file.
TypeScriptのプロジェクトを初期化し、tsconfig.jsonを作成します。
一緒に他のオプションをつけて実行すると、そのオプションを適用したtsconfig.jsonを作成してくれます。

--jsx KIND

Specify JSX code generation: 'preserve' or 'react'
preserveを指定するとそのままのjsxの書き方で、reactを指定するとReactのメソッドに変換されます

--jsxFactory

Specify the JSX factory function to use when targeting 'react' JSX emit, e.g. 'React.createElement' or 'h'.
JSXファクトリーを指定することができます。

--lib

Specify library files to be included in the compilation:
'es5' 'es6' 'es2015' 'es7' 'es2016' 'es2017' 'dom' 'dom.iterable' 'webworker' 'scripthost' 'es2015.core' 'es2015.collection' 'es2015.generator' 'es2015.iterable' 'es2015.promise' 'es2015.proxy' 'es2015.reflect' 'es2015.symbol' 'es2015.symbol.wellknown' 'es2016.array.include' 'es2017.object' 'es2017.sharedmemory' 'es2017.string'

コンパイルに含めるライブラリファイルを指定することができます。

--mapRoot LOCATION

Specify the location where debugger should locate map files instead of generated locations.
デバッガーが参照するマップファイルの場所を指定することができます。
Chromeなどでjsをデバッグするときなどにjsから参照する.mapファイルを任意の場所を見るように変更できます

--maxNodeModuleJsDepth

The maximum dependency depth to search under node_modules and load JavaScript files
--allowJSを指定しているときのnode_modulesの依存の深さの最大値を指定できるようです。(いまいち動作が不明なので誰か解説求む)

-m KIND, --module KIND

Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'
コード変換のモジュールの指定
モジュールの管理をcommonjsやamdなどの形式で行うか設定できます

--moduleResolution STRATEGY

Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).
モジュールの解決方法を指定することができます。

--newLine NEWLINE

Specify the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix).
ファイルを出力する際の改行コードをCRLFかLFか指定することができます。

--noEmit

Do not emit outputs.
ファイルを出力しないようにします。
コンパイラでのエラーチェックのみを行います。

--noEmitOnError

Do not emit outputs if any errors were reported.
エラーが合った場合ファイルを出力しないようにします。
TypeScriptではオプションでエラーの規則を一部変更できますが、エラーが発生した時でもJSファイルを生成します。
エラーが発生した場合にはこのオプションを有効にしておけば、JSファイルの生成を行わないようにすることができます。

--noFallthroughCasesInSwitch

Report errors for fallthrough cases in switch statement.
case文でのフォールスルーがある場合に、エラーを出力するようにします。
次のcase 1のようにbreakがなくてそのまま次のcaseも実行されるような場合です。

switch (num){
    case 1:
        console.log(1);
    case 2:
        console.log(2);
        break;
}

次のエラーが出力されます。

error TS7029: Fallthrough case in switch.

--noImplicitAny

Raise error on expressions and declarations with an implied 'any' type.
厳密に型を決めたいとき、暗黙のany型は全てエラーとすることができます。

--noImplicitReturns

Report error when not all code paths in function return a value.
TypeScriptではデフォルトではreturnが実行されないパターンや、returnのみのときは正常にコンパイルされます。
そういったパターンに該当するコードがある場合にこのオプションを有効にすることでエラーを発生させることができます。
例えば次のようなパターンです。

// sの値によっては文字列が返されない
public strA(s): string {
    if (s) {
        return "";
    }
}

// 何も値を返さないリターン
public strB(s): string {
    return;
}

次のエラーを発生させることができます。

error TS7030: Not all code paths return a value.

--noImplicitThis

Raise error on 'this' expressions with an implied 'any' type.
thisに型を指定していない場合エラーを発生させることができます。

test(1);
function test(this: void, a: number): void {
    console.log(this);
    console.log(a);
}

上記のコードで、this: voidの宣言を行わないと次のエラーが発生します。

error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

--noImplicitUseStrict

Do not emit 'use strict' directives in module output.
use strictを出力に含めないようにします。

--noUnusedLocals

Report errors on unused locals.
このオプションを有効にすると、未使用のローカル変数が存在するときに次のようなエラーを発生させることができます。

error TS6133: 'address' is declared but never used.

--noUnusedParameters

Report errors on unused parameters.
次のコードのように定義した引数が使用されないときにエラーを発生させることができます。

function unused(a: number): void {
    //    
}
error TS6133: 'a' is declared but never used.

--outDir DIRECTORY

Redirect output structure to the directory.
コンパイル後に掃き出すファイルの出力先ディレクトリを任意の場所に指定することができます

--outFile FILE

Concatenate and emit output to single file.
連結し、単一のファイルとして出力
FILEに指定した名前で複数の.tsファイルをひとつのファイルにまとめることができます。
注意として、上のoutDirの影響は受けません。

--preserveConstEnums

Do not erase const enum declarations in generated code.
コンパイル後のコードからconst enumの宣言を消さないようにします。
例えば以下のようにオプション無しとオプション無しでコンパイル結果が変わります。

enum E { X, Y, Z }
const enum ConstE { X, Y, Z }

console.log(E.X);
console.log(ConstE.X);
オプション無し
var E;
(function (E) {
    E[E["X"] = 0] = "X";
    E[E["Y"] = 1] = "Y";
    E[E["Z"] = 2] = "Z";
})(E || (E = {}));
console.log(E.X);
console.log(0 /* X */);
オプション付き
var E;
(function (E) {
    E[E["X"] = 0] = "X";
    E[E["Y"] = 1] = "Y";
    E[E["Z"] = 2] = "Z";
})(E || (E = {}));
var ConstE;
(function (ConstE) {
    ConstE[ConstE["X"] = 0] = "X";
    ConstE[ConstE["Y"] = 1] = "Y";
    ConstE[ConstE["Z"] = 2] = "Z";
})(ConstE || (ConstE = {}));
console.log(E.X);
console.log(0 /* X */);

--pretty

Stylize errors and messages using color and context. (experimental)
エラーの箇所を色付きで表示してくれます。
次のように表示されます。

21 str.nonexistentProperty();
       ~~~~~~~~~~~~~~~~~~~
src/main.ts(21,5): error TS2339: Property 'nonexistentProperty' does not exist on type 'string'.

ここだと波が赤くなります。

-p DIRECTORY, --project DIRECTORY

Compile the project in the given directory.
tsconfig.jsonが存在するディレクトリを指し示すことによってそこのtsconifg.jsonの設定でコンパイルできます。

--reactNamespace

Specify the object invoked for createElement and __spread when targeting 'react' JSX emit
Reactを使用するときのcreateElementと__spreadの呼び出しを指定できます。

--removeComments

Do not emit comments to output.
出力からコメントを削除
元の.tsファイルのコメントを入れないで出力することが出来ます。

--rootDir LOCATION

Specify the root directory of input files. Use to control the output directory structure with --outDir.
入力ファイルのルートディレクトリを指定、--outDirで出力場所の変更をするときに使用します。

--skipLibCheck

Skip type checking of declaration files.
型定義ファイルの整合性チェックを行わないようにします。
参考

--sourceMap

Generates corresponding '.map' file.
関連する.mapファイルを生成します。
このオプションを指定するとデバッグをするときにjs側から、.mapファイルを参照し、元の.tsファイルと結びつけてくれます

--sourceRoot LOCATION

Specify the location where debugger should locate TypeScript files instead of source locations.
デバッガーが見るソースファイルの場所を指定します。

--strictNullChecks

Enable strict null checks.
厳密なnullチェックを行います。
公式からの引用で、次のようにエラーになります。

// Compiled with --strictNullChecks
let x: number;
let y: number | undefined;
let z: number | null | undefined;
x = 1;  // Ok
y = 1;  // Ok
z = 1;  // Ok
x = undefined;  // Error
y = undefined;  // Ok
z = undefined;  // Ok
x = null;  // Error
y = null;  // Error
z = null;  // Ok
x = y;  // Error
x = z;  // Error
y = x;  // Ok
y = z;  // Error
z = x;  // Ok
z = y;  // Ok

通常時はnumber、number | undefined、number | null | undefinedは同義とみなされますが、厳密なnullチェックを行う場合、全て別の型として扱います。

--suppressImplicitAnyIndexErrors

Suppress noImplicitAny errors for indexing objects lacking index signatures.
インデクサアクセス時、値が見つからない時のnoImplicitAnyエラーを抑制します。

-t VERSION, --target VERSION

Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'
コンパイル出力のECMAScriptのバージョンを指定できます。

--traceResolution

Enable tracing of the name resolution process.
コンパイル時、モジュールのファイルの解決のプロセスを表示することができます。

--types

Type declaration files to be included in compilation.
コンパイルに含める宣言ファイルを指定できます。

-v, --version

Print the compiler's version.
バージョン表示

-w, --watch

Watch input files.
入力ファイル監視します。
そのファイルを保存したときにコンパイルが走るようになります

@<file>

Insert command line options and files from a file.
ファイルに記したコマンドラインオプションとファイルを挿入します。
細かい記述をしたものをファイルにまとめて、長いコマンドを打つことなくコンパイルしたりできます

参考

@vvakameさんのTypeScript変更点の記事
https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript
https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Compiler%20Options.md
https://github.com/typescript-ninja/typescript-in-definitelyland
http://www.buildinsider.net/language/quicktypescript/01

89
84
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
89
84