TypeScript Tutorial: Getting Started with TypeScript
What TypeScript is, why it exists, and how to write your first typed program.
What is TypeScript?
TypeScript is JavaScript with **static types**. It compiles to plain JavaScript, so it runs everywhere JavaScript runs — browsers, Node.js, Deno, Bun.
```typescript
// JavaScript — no types, errors caught only at runtime
function add(a, b) {
return a + b;
}
// TypeScript — types checked at compile time
function add(a: number, b: number): number {
return a + b;
}
add(1, 2); // ✓ fine
add("1", 2); // ✗ Error: Argument of type 'string' is not assignable to type 'number'
```
TypeScript catches bugs **before your code runs**. That's the core value.
---
Type Annotations
You annotate variables, parameters, and return values with `: Type`:
```typescript
let name: string = "Alice";
let age: number = 30;
let active: boolean = true;
function greet(name: string): string {
return `Hello, ${name}!`;
}
```
---
Type Inference
TypeScript is smart — it can infer types without annotations:
```typescript
let city = "London"; // inferred as string
let count = 42; // inferred as number
let flag = true; // inferred as boolean
city = 123; // Error: Type 'number' is not assignable to type 'string'
```
You don't need to annotate everything. TypeScript infers from context.
---
What's Next?
In the next lesson you'll learn the full set of TypeScript's **basic types** — primitive types, arrays, tuples, and special types like `any`, `unknown`, and `never`.
What you'll learn in this TypeScript getting started with typescript tutorial
This interactive TypeScript tutorial has 12 hands-on exercises. Estimated time: 10 minutes.
- Why TypeScript exists — JavaScript is great until a typo causes a production bug at 2am. TypeScript catches mistakes before your code runs.
- Type annotation syntax — : type — Add `: type` after a variable name or parameter to annotate its type. TypeScript checks every use against the annotation…
- Type inference — TypeScript reads your code — TypeScript infers types from initial values. You don't need to annotate every variable — the compiler figures it out.
- Function return types — Annotating a function's return type creates a contract: TypeScript verifies every `return` statement matches the declare…
- TypeScript vs JavaScript — same runtime — TypeScript compiles to plain JavaScript. All the type annotations disappear — there's zero runtime overhead. The types o…
- Strict mode — maximum safety — TypeScript's `strict: true` option enables the strongest type checking. It catches more bugs at the cost of being more e…
- Arrays and type inference — TypeScript infers array types from their elements. Operations on typed arrays are checked — you can't push a wrong type …
- Objects — typing the shape — TypeScript checks object shapes. If you define a variable as having `{ name: string; age: number }`, TypeScript enforces…
- Union types — either/or — A union type `A | B` means a value can be either type A or type B. You must handle both possibilities.
- Optional properties — ? in interfaces — Mark a property with `?` to make it optional. TypeScript tracks that it might be `undefined` and requires you to handle …
- Type aliases — name your types — A `type` alias gives a name to any type — primitives, objects, unions, arrays. Use them to make complex types readable a…
- The compiler catches real bugs — The best way to appreciate TypeScript is to see it catch an actual bug. Here's a common JavaScript mistake that TypeScri…
TypeScript Getting Started with TypeScript concepts covered
- What is TypeScript?
- Type Annotations
- Type Inference
- What's Next?