All Guides

TypeScript Einrichtung und Konfiguration

Fuegen Sie TypeScript zu Ihrem Projekt hinzu und konfigurieren Sie tsconfig.json.

Beginner10 Min.

Setup Steps

1. Install TypeScript:

npm install -D typescript

2. Create tsconfig.json:

npx tsc --init

3. Recommended tsconfig.json settings:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "declaration": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

4. Compile:

npx tsc

5. Watch mode compilation:

npx tsc --watch

6. Run TypeScript with Node.js:

npm install -D tsx
npx tsx src/index.ts

7. Type definition files:

npm install -D @types/node @types/express

8. package.json scripts:

"scripts": {
  "build": "tsc",
  "dev": "tsx watch src/index.ts",
  "start": "node dist/index.js"
}