✅ TypeScript 開発環境構築手順(Yarn使用・Windows)

前提:Node.js はインストール済み

1. Yarn のインストール(まだの場合)

npm install --global yarn

2. プロジェクトディレクトリの作成

mkdir my-typescript-app
cd my-typescript-app

3. package.json の作成

yarn init -y

4. TypeScript のインストール(開発依存)

yarn add typescript --dev

5. TypeScript 設定ファイルの作成

npx tsc --init

以下のように tsconfig.json を編集(必要に応じて):

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist",
    "rootDir": "src"
  }
}
tsconfig.json の compilerOptions 解説

6. src ディレクトリを作成してコードを書く

mkdir src

src/index.ts の例:

const message: string = "Hello, TypeScript!";
console.log(message);

7. ビルド用スクリプトの追加

package.json に以下を追加:

"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}

8. コンパイルと実行

yarn build
yarn start

🎉 これで開発環境は完成です!

必要があれば、ESLintやPrettier、Reactなどの導入方法も紹介できます。