Getting Started with TypeScript for Absolute Beginners

A beginner-friendly introduction to TypeScript, explaining key concepts and showing simple examples.

TypeScript is a programming language that builds on JavaScript by adding types. It helps you write safer code by catching errors before you run your program. This tutorial is designed for absolute beginners who want to learn the basics of TypeScript quickly and clearly.

One important concept in TypeScript is type annotations. When you create variables, you can specify what kind of values they should hold, like numbers or strings. This makes your code easier to understand and can prevent many common bugs.

typescript
let age: number = 25;
let name: string = "Alice";

// This will cause an error:
age = "twenty-five";  // Error: Type 'string' is not assignable to type 'number'.

In the example above, we declared a variable 'age' with the type 'number' and assigned it the value 25. If you try to assign a string like "twenty-five" to 'age', TypeScript will give an error. This error means you are trying to put a value of the wrong type into a variable. It happens because TypeScript expects 'age' to always be a number. To fix this error, make sure you only assign numbers to 'age', or change the variable's type if different kinds of values are needed.

Using TypeScript gives you immediate feedback about potential mistakes in your code, which helps you catch bugs early. As you learn more, you will discover many other useful features, like interfaces and classes. For now, practicing simple type annotations is a great first step.