0
0
Typescriptprogramming~5 mins

Why type annotations are needed in Typescript

Choose your learning style9 modes available
Introduction

Type annotations help us tell the computer what kind of data we expect. This stops mistakes before the program runs.

When you want to catch errors early before running the program.
When working with other people so everyone understands the data types.
When your program gets bigger and you want to keep it organized.
When you want your code editor to help you with suggestions and warnings.
When you want to make your code easier to read and maintain.
Syntax
Typescript
let variableName: type = value;

The colon : is used to add a type after the variable name.

Common types are string, number, and boolean.

Examples
This means age must always be a number.
Typescript
let age: number = 25;
This means name must always be text.
Typescript
let name: string = "Alice";
This means isStudent can only be true or false.
Typescript
let isStudent: boolean = true;
Sample Program

This program uses a type annotation to say name must be a string. It then prints a greeting.

Typescript
function greet(name: string) {
  return `Hello, ${name}!`;
}

console.log(greet("Bob"));
OutputSuccess
Important Notes

Type annotations do not change how the program runs, but help catch mistakes early.

You can let TypeScript guess the type, but adding annotations makes your code clearer.

Summary

Type annotations tell the computer what kind of data to expect.

They help find errors before running the program.

They make code easier to understand and maintain.