100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

Setting Up a TypeScript Environment

A practical guide to installing TypeScript, initializing a project with tsconfig.json, and compiling and running your first .ts file.

Introduction to TypeScriptBeginner8 min readJul 8, 2026
Analogies

1. Introduction

Before writing TypeScript code, you need a working development environment: Node.js and npm installed on your machine, the TypeScript compiler installed as a package, and a configuration file (tsconfig.json) that tells the compiler how to check and build your project.

🏏

Cricket analogy: Before a team can play, you need the ground prepared, the pitch report ready, and the match rules confirmed — similarly, before writing TypeScript you need Node.js, npm, and a tsconfig.json that sets the rules for checking your code.

This lesson walks through the typical steps: installing TypeScript, generating a config file, writing a simple .ts file, and compiling and running it with Node.js.

🏏

Cricket analogy: Just as a net session walks a batter through stance, backlift, and shot selection step by step, this lesson walks through installing TypeScript, generating a config, writing a .ts file, and running it.

2. Syntax

typescript
# Install Node.js first (from nodejs.org), then install TypeScript as a dev dependency
npm init -y
npm install --save-dev typescript

# Generate a default tsconfig.json file
npx tsc --init

# Compile a single file
npx tsc hello.ts

# Run the compiled JavaScript with Node.js
node hello.js

3. Explanation

TypeScript is distributed as an npm package, so the standard setup uses Node.js and npm (or yarn/pnpm) to install it into a project, typically as a devDependency since it's only needed during development/build, not at runtime. Running npx tsc --init generates a tsconfig.json file with common compiler options (like target, module, and strict) that control how your TypeScript is checked and compiled.

🏏

Cricket analogy: Installing TypeScript via npm as a devDependency is like a team hiring a specialist analyst only for pre-season prep, not for matchday; running npx tsc --init is like drafting the season's playing conditions document (target, module, strict) up front.

Once configured, running tsc compiles every .ts file covered by tsconfig.json into JavaScript, placing the output according to the outDir setting (or next to the source file if unset). Many developers also install ts-node, a tool that compiles and runs TypeScript directly in one step during development, without a separate compile phase.

🏏

Cricket analogy: Running tsc compiles every .ts file the way a groundstaff prepares every pitch listed in the season fixtures, placing finished output in outDir; ts-node is like a practice net that lets a batter face live bowling immediately, without separate preparation.

A common gotcha: installing TypeScript globally (npm install -g typescript) works for quick experiments, but production and team projects should install it locally as a devDependency in each project. This ensures everyone on the team — and your CI pipeline — uses the exact same TypeScript version, avoiding 'works on my machine' compiler version mismatches.

4. Example

typescript
// File: greet.ts
function greet(name: string, times: number = 1): string[] {
  const messages: string[] = [];
  for (let i = 0; i < times; i++) {
    messages.push(`Hello, ${name}!`);
  }
  return messages;
}

const result = greet("TypeScript", 3);
result.forEach((msg) => console.log(msg));

// Compile with: npx tsc greet.ts
// Run with:     node greet.js

5. Output

text
Hello, TypeScript!
Hello, TypeScript!
Hello, TypeScript!

6. Key Takeaways

  • TypeScript requires Node.js and npm to install and run its compiler tooling.
  • Install TypeScript locally per project with 'npm install --save-dev typescript' for consistent versions across a team.
  • 'npx tsc --init' generates a tsconfig.json that controls compiler behavior (target, module, strict, outDir, etc.).
  • Running 'tsc' compiles .ts files into plain .js files, which Node.js or a browser can then execute.
  • Tools like ts-node let you run TypeScript directly during development without a separate manual compile step.

Practice what you learned

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#SettingUpATypeScriptEnvironment#Setting#Environment#Syntax#Explanation#StudyNotes#SkillVeris