1️⃣ What Are Data Types in TypeScript?
Data types define what kind of value a variable can store. In TypeScript, variables must have a type to prevent errors.
✔ Example:
let name: string = "Alice"; // Only a string can be assigned
let age: number = 25; // Only a number can be assigned
let isActive: boolean = true; // Only true or false can be assigned
🚀 Why use data types?
✔ Prevents errors before running the program
✔ Makes code more readable
✔ Improves performance by detecting mistakes early
2️⃣ Built-in Data Types in TypeScript
TypeScript provides the following basic data types:
Data Type | Example | Description |
---|---|---|
string | “Hello” | Stores text values |
number | 25, 3.14 | Stores integer and floating-point numbers |
boolean | true, false | Stores true or false values |
any | anything | Can store any type of value (not recommended) |
unknown | anything | Similar to any but safer |
void | undefined | Used for functions that do not return a value |
never | never | Represents values that never occur |
array | [1, 2, 3] | Stores a collection of values |
tuple | [1, “Hello”] | A fixed-length array with different types |
enum | enum Color {Red, Green, Blue} | Stores a set of named constants |
object | { key: “value” } | Stores complex data structures |
Let’s understand each type with examples.
3️⃣ string Data Type (Text Values)
The string type is used for text values. You must enclose strings in single ( ‘ ) or double ( ” ) quotes.
✔ Example:
let firstName: string = "John";
let message: string = "Welcome to TypeScript!";
console.log(firstName, message);
✔ Using template literals (backticks ` `):
let user: string = "Alice";
let greeting: string = `Hello, ${user}!`; // ✅ Works with variables
console.log(greeting);
✅ Best Practice: Always use string for text values instead of any.
4️⃣ number Data Type (Numbers & Decimals)
The number type stores both integers and floating-point numbers.
✔ Example:
let age: number = 25;
let price: number = 99.99;
let hex: number = 0xff; // Hexadecimal
let binary: number = 0b1010; // Binary
let octal: number = 0o123; // Octal
console.log(age, price, hex, binary, octal);
✅ Best Practice: Use number instead of any for calculations.
5️⃣ boolean Data Type (True or False)
The boolean type stores true or false values.
✔ Example:
let isLoggedIn: boolean = true;
let hasPermission: boolean = false;
console.log(isLoggedIn, hasPermission);
🚀 Why use boolean
?
✔ Makes code more readable and logical
✔ Prevents mistakes when checking conditions
6️⃣ any Data Type (Avoid Using It!)
The any type allows a variable to hold any kind of value. It removes type safety, so avoid using it.
✔ Example:
let data: any = 42;
data = "Hello"; // ✅ Allowed, but not recommended
data = true;
console.log(data);
🚫 Why avoid any?
✔ No type checking – Errors can go unnoticed
✔ Code becomes unpredictable
✅ Best Practice: Use specific types instead of any.
7️⃣ unknown Data Type (Safer than any)
The unknown type is like any, but it forces type checking before usage.
✔ Example:
let value: unknown;
value = 42;
value = "Hello";
// value.toUpperCase(); // ❌ Error: Must check type first
if (typeof value === "string") {
console.log(value.toUpperCase()); // ✅ Works
}
✅ Best Practice: Prefer unknown over any when you don’t know the exact type.
8️⃣ void Data Type (For Functions Without Return Values)
The void type is used when a function does not return anything.
✔ Example:
function logMessage(): void {
console.log("This function returns nothing!");
}
✅ Best Practice: Use void for functions that only perform actions (like logging data).
9️⃣ never Data Type (For Impossible Values)
The never type is used for functions that never return a value (like errors or infinite loops).
✔ Example:
function throwError(message: string): never {
throw new Error(message);
}
✅ Best Practice: Use never for functions that always throw errors.
🔟 array Data Type (Lists of Values)
Arrays store multiple values of the same type.
✔ Example:
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
console.log(numbers, names);
✔ Using Array<T> syntax:
let scores: Array<number> = [100, 90, 80];
console.log(scores);