Introduction
Type annotations help us tell the computer what kind of data we expect. This stops mistakes before the program runs.
Type annotations help us tell the computer what kind of data we expect. This stops mistakes before the program runs.
let variableName: type = value;The colon : is used to add a type after the variable name.
Common types are string, number, and boolean.
age must always be a number.let age: number = 25;
name must always be text.let name: string = "Alice";
isStudent can only be true or false.let isStudent: boolean = true;
This program uses a type annotation to say name must be a string. It then prints a greeting.
function greet(name: string) { return `Hello, ${name}!`; } console.log(greet("Bob"));
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.
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.
let age: number = 25;let age: number = 25;, the annotation number tells the computer to expect a number value.let variableName: type = value;.let name: string = 'Alice'; is correct.let count: number = 10; count = 'ten';
count is declared as number.'ten' (a string) to count violates the type annotation.function greet(name) {
return 'Hello, ' + name.toUpperCase();
}
greet(123);toUpperCase(), but 123 is a number.name: string tells TypeScript to expect a string and catch wrong calls like greet(123).name: string in function parameter -> Option Bfunction calculateArea(width, height) {
return width * height;
}width: number and height: number ensures only numbers are accepted, preventing errors.Pass the test and this pattern moves unknown