Labs ICT
โญ Pro Login

Type Annotation

Explicit type annotations are like putting labels on your variables. They tell TypeScript exactly what type you want, removing any doubt about what can go where. This is especially important when you're passing data around or working with complex objects. Think of it as writing the type definition directly in your code.

When Type Annotations Matter

Type annotations clarify intent and help the compiler catch errors early. The difference between `let` and `const` is important - `let` variables can change, `const` variables cannot (including their types). Type annotations are your safety net, making your code more reliable and easier to understand.


let counter: number = 0;
counter = 5; // TypeScript knows this is OK

const PI: number = 3.14159;
// PI = 5; // TypeScript error - cannot reassign

const username: string = "admin";
const isAuthenticated: boolean = true;

// Functions with type annotations
function add(a: number, b: number): number {
    return a + b;
}
    
Try it Yourself โ†’

๐Ÿงช Quick Quiz

How do you declare a variable with type annotation?