TypeScript ยท Chapter 1 of 44

TypeScript Introduction

TypeScript is a programming language built on top of JavaScript, created by Microsoft. It adds an optional static type system to JavaScript, which means you can describe the shape of your data and catch many mistakes before your code ever runs.

Under the hood, TypeScript code is compiled (or 'transpiled') into plain JavaScript, so it can run anywhere JavaScript runs: in browsers, on servers with Node.js, or in mobile apps. Because it is a superset of JavaScript, any valid JavaScript file is already valid TypeScript.

Why use TypeScript?

TypeScript catches type-related bugs at compile time instead of at runtime. It also improves editor support, giving you autocomplete, inline documentation, and safer refactoring in large codebases.

How TypeScript works

You write .ts files using JavaScript plus type annotations. The TypeScript compiler (tsc) checks your types and then outputs plain .js files that browsers and Node.js can execute directly.

Example 1 (typescript)
let message: string = "Hello, TypeScript!";
console.log(message);
Output
Hello, TypeScript!

The `: string` annotation tells TypeScript that message must always hold a string.

Example 2 (typescript)
function add(a: number, b: number): number {
  return a + b;
}
console.log(add(2, 3));
Output
5

The function signature declares that both parameters and the return value are numbers.

Key points

  • TypeScript is a superset of JavaScript with optional static types.
  • It was created and is maintained by Microsoft.
  • TypeScript compiles down to plain JavaScript.
  • Type checking happens at compile time, before the code runs.
๐Ÿ’ก Note: You do not need to rewrite your JavaScript from scratch โ€” TypeScript lets you add types gradually.

๐Ÿ“ Quick Quiz

1. What is TypeScript?

2. Who created TypeScript?

3. What does the TypeScript compiler output?