まとめ

TypeScriptの導入、コンパイル、ディレクトリ構成についての覚書

前提

OS: Windows 10 Editor: VS code

Setup for TypeScript

  1. Install TypeScript 事前にNode.jsをインストールしておけば、コマンド一発でインストールできる。
npm install -g typescript
  1. Create Project initコマンドでTypeScriptの設定ファイルtsconfig.jsonを生成し、 src/buildディレクトリを用意する。 tscconfig.jsonにて、 “outDir”: “./build”を指定することでビルド結果の出力先を指定できる。
$ mkdir workspace
$ tsc --init
$ mkdir build
$ mkdir src
  1. Build and Run src/index.tsを作成し、以下のコードを記載する。
export function hello(word: string = world): string{
    return `Hello ${word}`;
}
console.log(hello("world"));

tscコマンドでTSファイルをJSファイルにコンパイルし、 実行できる。

$ tsc; node build/index.js
Hello world
$ tree
./workspace
│  tsconfig.json
│
├─build
│      index.js
└─src
       index.ts