Skip to content

Instantly share code, notes, and snippets.

@kazz12211
Created March 7, 2019 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kazz12211/f53027d45e85dfff2b69c84a4258025a to your computer and use it in GitHub Desktop.
Save kazz12211/f53027d45e85dfff2b69c84a4258025a to your computer and use it in GitHub Desktop.
Learning TypeScript - First Step

Learning TypeScript

TypeScriptのインストール

Nodeがインストールされている必要がある。

$ npm install -g typescript

プロジェクトを作成する

$ mkdir HelloWorld
$ cd HelloWord
$ code .

helloworld.tsを書く

let message : string = "Hello World";
console.log(message);

TypeScriptコードをコンパイルする

$ tsc helloworld.ts

出力ファイル(helloworld.js)はhelloworld.tsを同じディレクトリに書かれる。

helloworld.jsを実行する

$ node helaoworld.js
Hello Worla

tsconfia.jsonの編集

コンパイルされた出力ファイルの保存先を変更するには outDir をtsconfig.tsファイルに設定し

{
    "compileOptions" : {
        "target" : "es5",
        "module" : "commonjs",
        "outDir": "out"
    }
}

引数無しでtscを実行する。

$ tsc

引数にファイル名を指定するとtsconfig.jsonは無視される。

デバッギング

sourceMap = true をtsconfig.jsonに設定し

{
    "compileOptions" : {
        "target" : "es5",
        "module" : "commonjs",
        "outDir": "out",
        "sourceMap": true
    }
}

tscを実行してコンパイルを行う。

そしてターゲットファイル(helloworld.js)を選んで、DebugメニューのStart Debuggingを選択する。(デフォルトではデバッガーとしてNode.jsを使用することになる)

Debug Consoleの表示。

/usr/local/bin/node --inspect-brk=5894 out/helloworld.js 
Debugger listening on ws://127.0.0.1:5894/3b3993c6-5eaa-4cf9-8aa8-3f3ab28b5223
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment