codez.guru

What are Type Annotations?

Type annotations tell TypeScript the kind of value a variable will hold.

let username: string = "Ziv";
let age: number = 30;
let isAdmin: boolean = true;

You can declare a type with : after the variable name.


Core Primitive Types

string, number, boolean

let firstName: string = "Ada";
let score: number = 99.5;
let active: boolean = false;

Arrays and Tuples

Arrays

let fruits: string[] = ["apple", "banana"];
let scores: number[] = [10, 20, 30];

Or using generic notation:

let values: Array<number> = [1, 2, 3];

Tuples

Tuples are fixed-length arrays with known types:

let user: [string, number] = ["Alex", 42];

Any vs Unknown

any disables type checking:

let data: any = "Could be anything";
data = 123; // valid
data = true;

unknown is safer — must be checked before use:

let input: unknown = "hello";

if (typeof input === "string") {
  console.log(input.toUpperCase());
}

> Prefer unknown over any for external data when you want safety.


Void, Null, and Undefined

void = no return

function logMessage(): void {
  console.log("Hello");
}

null and undefined

You can allow them explicitly:

let maybe: string | null = null;
let value: number | undefined = undefined;

Summary

  • Use type annotations to clearly declare value types
  • Master core types: string, number, boolean, array, tuple
  • Avoid any unless absolutely necessary — prefer unknown when unsure
  • Understand when to use void, null, and undefined

Next up: how functions work in TypeScript, with full type inference and safety.