Typescript の tsconfig.json の簡単作成方法と設定について

typescriptで開発をしていると必要になってくるのが、tsconfig.json です。ですが、これに何を書いたらいいのか、どんな意味なのかさっぱりでしたので、簡単に残しておきます。

1. tsconfig.json の自動生成

以下のコマンドで、生成できます。

tsc --init

実行すると、こんな感じで成功するはずです。

D:\playground\ts-init>tsc --init
message TS6071: Successfully created a tsconfig.json file.

2. 自動生成された tsconfig.json の中身

こんな感じで出力されます。

    {
      "compilerOptions": {
        /* Basic Options */
        "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
        // "lib": [],                             /* Specify library files to be included in the compilation. */
        // "allowJs": true,                       /* Allow javascript files to be compiled. */
        // "checkJs": true,                       /* Report errors in .js files. */
        // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
        // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
        // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
        // "sourceMap": true,                     /* Generates corresponding '.map' file. */
        // "outFile": "./",                       /* Concatenate and emit output to single file. */
        // "outDir": "./",                        /* Redirect output structure to the directory. */
        // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
        // "composite": true,                     /* Enable project compilation */
        // "removeComments": true,                /* Do not emit comments to output. */
        // "noEmit": true,                        /* Do not emit outputs. */
        // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
        // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
        // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
        /* Strict Type-Checking Options */
        "strict": true,                           /* Enable all strict type-checking options. */
        // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
        // "strictNullChecks": true,              /* Enable strict null checks. */
        // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
        // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
        // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
        // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
        // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
        /* Additional Checks */
        // "noUnusedLocals": true,                /* Report errors on unused locals. */
        // "noUnusedParameters": true,            /* Report errors on unused parameters. */
        // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
        // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
        /* Module Resolution Options */
        // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
        // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
        // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
        // "typeRoots": [],                       /* List of folders to include type definitions from. */
        // "types": [],                           /* Type declaration files to be included in compilation. */
        // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
        "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
        // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
        /* Source Map Options */
        // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
        // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
        // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
        // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
        /* Experimental Options */
        // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
        // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
      }
    }

3. それぞれのパラメーターと設定値

ちょっと数が多すぎるので、自分が必要なところだけ記載しています。 (少しずつ追記していきます)。

target

対象とするECMAScriptのバージョンを指定します。

‘ES3’ (default), ‘ES5’, ‘ES2015’, ‘ES2016’, ‘ES2017′,’ES2018’, ‘ESNEXT’ から選択します。

初期値は “ES3″です。

module

生成するモジュールを指定します。

‘none’, ‘commonjs’, ‘amd’, ‘system’, ‘umd’, ‘es2015’, ‘ESNext’ から選択します。

初期値は、targetES3 または ES5のときはcommonjsが設定されます。

declaration

trueのとき、*.d.tsファイルを生成します。

sourceMap

trueのとき、マップファイル*.js.mapを生成します。

rootDir

tscが処理を行う、ルートディレクトリを指定します。

outDir

コンパイル済みファイルの出力先ディレクトリを指定します。

4. 自動生成された tsconfig.json には出力されていないけど、tsconfig.json に記載可能なパラメタ

tsc --init で全部出してくれないの?と思ってしまいますが。あるんです、他にもパラメタが。

files

tscでコンパイルする対象の*.tsファイルを指定します。 例はこんな感じです。

    "files": [
        "./DailyLogRotate.ts",
        "./main.ts"
    ]

include

というのがあるそうですが、使ってないので、よく分かりません。使ったら追記します。

5. 自分がよく使う tsconfig.json

自分はこんな感じで使っています。

{
    "compilerOptions": {
        "target": "es2018",
        "module": "commonjs",
        "sourceMap": true,
        "rootDir": ".",
        "outDir": "../build/"
    },
    "files": [
        "./DailyLogRotate.ts",
        "./main.ts"
    ]
}

この状態で、ソースファイルが格納しているフォルダでtsc(引数なし)を実行すると、filesに指定したTypeScriptファイルをコンパイルし、../build/フォルダに*.jsファイルと*.js.mapファイルが出力されます。

6. 公式ドキュメント

tsconfig.json の公式ドキュメントは以下にあります。

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

[商品価格に関しましては、リンクが作成された時点と現時点で情報が変更されている場合がございます。]

実践TypeScript【電子書籍】[ 吉井健文 ]
価格:3726円 (2019/7/23時点)

[

楽天で購入

](https://hb.afl.rakuten.co.jp/hgc/18648e8b.7d833591.18648e8c.ed1bab3c/?pc=https%3A%2F%2Fitem.rakuten.co.jp%2Frakutenkobo-ebooks%2F5456816fd8833397a6aa8b80647873ad%2F%3Fscid%3Daf_pc_bbtn&m=http%3A%2F%2Fm.rakuten.co.jp%2Frakutenkobo-ebooks%2Fi%2F18341447%2F%3Fscid%3Daf_pc_bbtn&link_type=picttext&ut=eyJwYWdlIjoiaXRlbSIsInR5cGUiOiJwaWN0dGV4dCIsInNpemUiOiIyNDB4MjQwIiwibmFtIjoxLCJuYW1wIjoicmlnaHQiLCJjb20iOjEsImNvbXAiOiJkb3duIiwicHJpY2UiOjEsImJvciI6MSwiY29sIjoxLCJiYnRuIjoxfQ==)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です