Skip to content

TypeScript Introduction and Setup

TypeScript is a superset of JavaScript. That means all valid JavaScript is valid TypeScript, plus TypeScript adds features like static typing and compile-time checks.

You write .ts files, then the TypeScript compiler (tsc) converts them into .js files.


graph TD A[Write .ts file] --> B[Type Checking] B --> C[Transpile to .js] C --> D[Run in Browser or Node.js]
  • Type errors are found before runtime.
  • Runtime still executes JavaScript, not TypeScript.

Terminal window
npm install -g typescript
tsc --version

If your project already has TypeScript in devDependencies, prefer local execution:

Terminal window
npx tsc --version

Terminal window
tsc --init

This creates tsconfig.json, which controls TypeScript behavior.


{
"compilerOptions": {
"strict": true,
"target": "ES2020",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"moduleResolution": "Bundler",
"skipLibCheck": true
},
"include": ["src"]
}

strict

Enables strong type checks. This is the most important option for real projects.

target

Decides what JavaScript version gets generated.

module

Controls module syntax in output.

outDir

Output folder for compiled JavaScript.


When strict is true, TypeScript enables checks like:

  • noImplicitAny
  • strictNullChecks
  • strictFunctionTypes
  • strictBindCallApply

These checks prevent many bugs in large codebases.


The tsconfig.json file is a configuration file that tells TypeScript how to compile your code. Think of it as the rulebook for the TypeScript compiler.

  • Which JavaScript version to output (ES2020, ES2015, etc.)
  • Module system (CommonJS, ESM, etc.)
  • Where to find source files
  • Where to put compiled JavaScript
  • How strict type checking should be
  • Which files to include or exclude

TypeScript will only work properly if tsconfig.json exists in your project root. Without it, the compiler won’t know your preferences.


A complete tsconfig.json has three main sections:

{
"compilerOptions": {
// Controls how TypeScript compiles code
},
"include": ["src"], // Which files to compile
"exclude": ["node_modules", "dist"] // Which folders to skip
}

These are settings that control the compilation behavior:

{
"compilerOptions": {
"strict": true,
"target": "ES2020",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true
}
}

Specifies which files TypeScript should compile:

{
"include": [
"src/**/*", // All files in src folder
"src/**/*.ts", // Only .ts files
"tests/**/*.test.ts" // Include test files
]
}

Tells TypeScript which folders/files to ignore:

{
"exclude": [
"node_modules",
"dist",
"build",
"**/*.spec.ts",
"**/*.test.ts"
]
}

strict

Master switch for all strict type checking. When true, enables: noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitThis.

target

What JavaScript version to output. Common values: “ES2020”, “ES2015”, “ES2022”. Higher targets = smaller output but less browser support.

module

Output module system. “ESNext” = modern import/export, “CommonJS” = require/module.exports.

moduleResolution

How to resolve import paths. “node” = npm-style resolution, “bundler” = for bundlers like webpack.

outDir

Folder where compiled .js files go. Usually ”./dist” or ”./build”.

rootDir

Tells TypeScript where source files start. Map folder structure: src/ → dist/

{
"compilerOptions": {
"declaration": true, // Generate .d.ts files for type info
"sourceMap": true, // Generate .map files for debugging
"noImplicitAny": true, // Error if type can't be inferred
"noUnusedLocals": true, // Error on unused variables
"noUnusedParameters": true, // Error on unused function params
"noImplicitReturns": true, // Error if not all code paths return
"allowJs": true, // Allow .js files in project
"checkJs": false, // Disable type checking in .js files
"forceConsistentCasingInFileNames": true, // Error on mismatched import casing
"resolveJsonModule": true, // Allow JSON imports
"esModuleInterop": true, // Compatibility for CommonJS modules
"skipLibCheck": true // Skip type checking of .d.ts files
}
}

{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "bundler",
"allowJs": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

Terminal window
# Uses tsconfig.json automatically
npx tsc
# Verbose output to see what's happening
npx tsc --listFiles
Terminal window
npx tsc --watch
# or
npx tsc -w

Very useful during development. The compiler runs every time you save a .ts file.

Terminal window
# Even when using tsconfig.json
npx tsc src/main.ts
Terminal window
npx tsc --noEmit

Useful in CI/CD pipelines to catch type errors without generating files.

Terminal window
npx tsc --showConfig

Shows the final merged configuration that TypeScript is using.



{
"include": [
"src/**/*", // Everything in src (recursive)
"src/**/*.ts", // Only .ts files
"src/pages/*.ts", // Only files directly in pages/
"src/**/*.{ts,tsx}" // Both .ts and .tsx files
],
"exclude": [
"**/node_modules/**",
"**/dist/**",
"**/*.test.ts",
"**/*.spec.ts"
]
}
{
"include": [
"src/**/*",
"tests/**/*.test.ts"
],
"exclude": [
"node_modules",
"dist",
"build",
".git",
".vscode"
]
}

Check:

  1. Is it in the include pattern?
  2. Is it excluded by exclude?
  3. Run: npx tsc --listFiles to see all files being processed

Check:

  • rootDir correctly points to source root
  • outDir is where you expect output
  • File structure: src/utils/helper.tsdist/utils/helper.js

Check:

  • moduleResolution setting (usually "node" or "bundler")
  • baseUrl for path mapping
  • paths configuration for aliases
Terminal window
npx tsc --showConfig -p .

Shows the complete final configuration being used.