Explicit type annotations help you tell the computer exactly what kind of data you expect. This makes your code clearer and helps catch mistakes early.
Explicit type annotations in Typescript
let variableName: type = value;The colon : is used to add the type after the variable name.
You can use explicit types for variables, function parameters, and return values.
age can only hold numbers.let age: number = 25;
name can only hold text (strings).let name: string = "Alice";
name and returns nothing (void).function greet(name: string): void { console.log(`Hello, ${name}!`); }
isActive can only be true or false.let isActive: boolean = true;
This program defines a function multiply that takes two numbers and returns their product. We use explicit type annotations for the parameters and the return type. Then we call the function and print the result.
function multiply(x: number, y: number): number { return x * y; } let result: number = multiply(4, 5); console.log(`The result is ${result}`);
Explicit types make your code safer and easier to read.
TypeScript can often guess types, but adding explicit types helps prevent errors.
You can use explicit types for arrays, objects, and more complex data too.
Explicit type annotations tell TypeScript exactly what type a variable or function uses.
They help catch mistakes and make code easier to understand.
Use them when you want clear, safe, and readable code.