SyntaxStudy
Sign Up
TypeScript Setting Up TypeScript with tsconfig.json
TypeScript Beginner 1 min read

Setting Up TypeScript with tsconfig.json

To use TypeScript in a project you need to install the TypeScript compiler and configure it through a tsconfig.json file. The compiler can be installed globally via npm or locally as a dev dependency. Once installed, running tsc --init generates a tsconfig.json with sensible defaults that you can customise for your project. The tsconfig.json file controls how TypeScript compiles your code. Key options include target (which JavaScript version to emit), module (the module system to use), strict (enables all strict type checks), outDir (where compiled files go), and rootDir (the source directory). Enabling strict mode is strongly recommended because it activates a set of checks — such as strictNullChecks and noImplicitAny — that catch the most common classes of bugs. For modern projects you typically use a build tool like Vite, esbuild, or ts-node for development, with tsc used primarily for type checking. Understanding tsconfig.json lets you fine-tune the compiler to match your environment, whether you are building a browser app, a Node.js server, or a shared library.
Example
// Terminal setup
// npm install --save-dev typescript
// npx tsc --init

// tsconfig.json (recommended strict config)
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "declaration": true,
    "sourceMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

// src/index.ts
const greet = (name: string): string => `Hello, ${name}!`;
console.log(greet("TypeScript"));

// Compile: npx tsc
// Run output: node dist/index.js

// Or use ts-node for direct execution:
// npx ts-node src/index.ts