前提:Node.js はインストール済み
npm install --global yarn
mkdir my-typescript-app
cd my-typescript-app
yarn init -y
yarn add typescript --dev
npx tsc --init
以下のように tsconfig.json
を編集(必要に応じて):
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"rootDir": "src"
}
}
tsconfig.json の compilerOptions 解説mkdir src
src/index.ts
の例:
const message: string = "Hello, TypeScript!";
console.log(message);
package.json
に以下を追加:
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
yarn build
yarn start
🎉 これで開発環境は完成です!
必要があれば、ESLintやPrettier、Reactなどの導入方法も紹介できます。