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?

TypeScriptの環境を準備する

0
Posted at

前提

(下書き)

Step1 - プロジェクト初期化

cd /workspace
npm init -y

Step2 - TypeScript, 型定義のインストール

npm i -D typescript tsx @types/node

Step3 - tsconfig.jsonの生成

npx tsc --init

よく使用するオプション

  {                                                                                                                                                                       
    "compilerOptions": {                                                                                                                                                
      "target": "ES2022",
      "module": "commonjs",                                                                                                                                               
      "strict": true,          // 厳格な型チェック
      "outDir": "./dist",                                                                                                                                                 
      "rootDir": "./src",                                                                                                                                                 
      "sourceMap": true
    }                                                                                                                                                                     
  }

Step4 - テストフレームワークインストール

npm i -D vitest

Step5 - 動作確認

npx tsx src/index.ts

オプション: package.json スクリプト追加

  "scripts": {
    "build": "tsc",                                                                                                                                                       
    "dev": "tsx src/index.ts",                                                                                                                                      
    "test": "vitest"                                                                                                                                                      
  }
npm run dev

Step6 - 型チェックも実施する

npm install -D tsc-watch
  "scripts": {
    "build": "tsc",
    "dev": "tsx --watch src/index.ts",
    "dev:strict": "tsc-watch --onSuccess \"node dist/index.js\"",
    "test": "vitest run"
  }
npm run dev:strict
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?