TypeScript — Get Started
What is TypeScript
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds static type checking to JavaScript, catching errors at compile time rather than runtime.
Installation
Install TypeScript globally with npm:
npm install -g typescript
Verify the installation:
tsc --version
# Version 5.x.x
Your first TypeScript file
Create a file called hello.ts:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
Compiling TypeScript
Use the tsc compiler:
# Compile a single file
tsc hello.ts
# This generates hello.js
Run the compiled JavaScript:
node hello.js
# Hello, Alice!
Watch mode
Auto-compile on file changes:
tsc --watch
Project initialization
Create a tsconfig.json for project-wide configuration:
tsc --init
This generates a configuration file with sensible defaults:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Compile the project
# Compile all files according to tsconfig.json
tsc
# Or use watch mode
tsc --watch
ts-node for quick testing
Run TypeScript directly without compiling:
npm install -g ts-node
# Run a TypeScript file directly
ts-node hello.ts
TypeScript Playground
Try TypeScript in the browser at typescriptlang.org/play — no installation required.
Project structure
my-project/
├── src/
│ ├── index.ts
│ ├── utils.ts
│ └── types.ts
├── dist/ # Compiled output
├── tsconfig.json
└── package.json
Basic types preview
// Type annotations
let name: string = "Alice";
let age: number = 30;
let isActive: boolean = true;
let items: string[] = ["a", "b", "c"];
// Type inference
let count = 10; // TypeScript infers: number
function add(a: number, b: number): number {
return a + b;
}
console.log(add(3, 4)); // 7
Next steps
- Set up a project with
tsc --init - Learn about Types — TypeScript's type system
- Build something small to practice
Mini Practice
- Install TypeScript and create your first
.tsfile - Compile it with
tscand run the output withnode - Try
ts-nodefor quick execution - Explore the TypeScript Playground online
Up Next
In the next lesson, you'll learn about Syntax — TypeScript's syntax rules and conventions.
Related Topics
Frequently Asked Questions about Get Started
What is Get Started in TypeScript?
Get Started is a fundamental concept in TypeScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Get Started?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Started.
Why is Get Started important in TypeScript?
Get Started is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.