0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Typescript] Compiler

Posted at

watchモード

↓いずれかのコマンドでwatchモードが開始し、変更がある度自動でコンパイルしてくれます。

% tsc ファイル名 --watch
% tsc ファイル名 -w

Image from Gyazo

停止はcontrol + cです。

tsconfig.json

↓コマンドでtsconfig.jsonというファイルが作成されます。

% tsc --init

その後tscコマンドを打つと、
全てのファイルを一括でコンパイルすることができます。

exclude

コンパイルしたくないファイルがあれば、tsconfig.json内でexcludeに指定しておきます。
exclude = 除外

tsconfig.json
  "exclude": [
    "index.ts",
    "*.spec.ts",
    "**/compiler.ts",
    "node_modules"
  ]

"index.ts"     = index.tsを除外
"*.spec.ts"    = spec.tsが拡張子のものを全て除外
"**/compiler.ts" = ルートディレクトリ外でも、全ての指定ファイルを除外
"node_modules"  = excludeを使う際、明示的に書いておくもの

include

コンパイルするファイルを指定する。
include = 含む

  "include": [

  ]
files

ワイルドカードの指定がができません。

  "files": [

  ]

unclude ー exclude +@ files

includeとfilesが無ければ全ファイルになります。

target

targetは、JavascriptのVer何にコンパイルされるかを指定するもの。
デフォルトはES3です。

lib

型の定義が載っているファイルの中で、どれを使うかを指定するのがlibです。
libが指定されていなかった場合は、targetによって変化します。
↓はES6のlibです。このようにlibを指定しても良いですし、targetがES6であれば指定しなくても
↓中身はこうなっています。

    "lib": [
       "ES6",
       "DOM",
       "DOM.Iterable",
       "ScriptHost"
    ]

allowJs   

Javascriptをコンパイラの対象にする

checkJs  

JavascriptファイルをTypescriptのようにエラーチェックする

jsx           

React.JSに使う

declaration       

型定義ファイル

declarationMap     

型定義ファイル

sourceMap

Map fileを作成するものです。
ブラウザでTypescriptのファイルを見たいときに使います。

tsconfig.json
"sourceMap": true

truにして%tscを打つ。
すると、検証のsourceからでもtsファイルを見れるようになります。

outDir 

TypescriptがJavascriptに変換されて出力されるとき、
その出力先を指定することができます。

rootDir

removeComents

コメントを消してくれるものです。

noEmit

Typescriptの型が正しく書けているかをチェックしてくれます。

downlevelIteration

targetがES3, ES5のときのみ使えます。
for-ofでの繰り返し処理をES3,S5にコンパイルしようとするとエラーが起きる可能性があります。
例えば、絵文字を使って文字列をループしようとしてエラーが起きたりします。
そういったことを↓で防ぐことができます。

tsconfig.json
"downlevelIteration": true

Iteration = 反復、繰り返し

noEmitOnError

エラーがあったらJavascriptにコンパイルしないというものです。

tsconfig.json
"noEmitOnError": true

strict

↓7項目を全てtrueにする。 ※コメントアウトしていても関係ない
noImplicitAny
strictNullChecks
strictFunctionTypes
strictBindCallApply
strictPropertyInitialization
noImplicitThis
alwaysStrict

noImplicitAny

Implicit = 暗黙
「暗黙的なanyはエラーを返しますよ」といったものです。
↓だとパラメーター 'message' の型は暗黙的に 'any' になります。というエラーが出ます。

index.ts
function echo(message) {
  return message;
}

(message) の後ろに: stringなどの型をつければ解決します。

strictNullChecks

厳しいnull型チェックです。
↓は、strictNullChecksがfalseだとok、trueだとエラーになります。

index.ts
let nullableMessage: string = null;
let undefinedableMessage: string = undefined;
let onlyNull: null = undefined;
let onlyUndefined: undefined = null;

strictFunctionTypes

classの継承時に起こり得るバグを防ぐものです。

strictBindCallApply

bindとcallとapplyに対して厳しい型のチェックをするものです。

strictPropertyInitialization

classを使うときに使用します。

noImplicitThis

thisが何を示しているかわからないときにエラーを返すものです。

alwaysStrict

コンパイル後のJavascriptファイルに**"use strict"**を付けてくれます。

Additional Checks

noUnusedLocals

使ってないローカル変数あれば教えてくれます。
↓は「'hello' が宣言されていますが、その値が読み取られることはありません。」という
↓エラーが出ます。

index.ts
function echo(message: string) {
  let hello = 'hello';
  return message;
}

ローカル変数内で定義されているため、他で使うこともありません。
よって、使い道がない無駄な変数だと判断されます。

noUnusedParameters

使えないパラメーターあれば教えてくれます。
↓は、name: stringにエラーが出ます。

index.ts
function echo(message: string, name: string) {
  let hello = 'hello';
  return message;
}

noImplicitReturns

暗黙的なreturnはだめいうエラーを出します。

index.ts
function echo(message: string): string | undefined {
  if (message) {
    return message;
  }
}

↑はelseの場合のreturnをちゃんと書け!というエラーが出ます。

index.ts
function echo(message: string): string | undefined {
  if (message) {
    return message;
  }
  return;
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?