Hey everyone π,
Iβm Masayeakh, a full stack dev from Bangladesh currently learning TypeScript to level up my JavaScript skills.
After diving deeper into TypeScript this week, I learned a few features that completely changed how I think about code β Union Types, Tuples, Enums, and Interfaces.
Letβs go through them one by one π
π 1. Union Types β When You Donβt Know Whatβs Coming
Sometimes you donβt know if a value will be a string or a number.
Thatβs where Union Types shine.
let score: number | string = 33;
score = "55";
TypeScript now lets score accept both numbers and strings β without losing type safety.
Hereβs a more practical example π
type User = { name: string; email: string; id: number; };
type Admin = { adminName: string; email: string; id: number; };
let person: User | Admin = { name: "Masayeakh", email: "m@gmail.com", id: 101 };
person = { adminName: "Masayeakh", email: "m@admin.com", id: 101 };
π§ Lesson:
Union types make your code flexible when multiple data types or structures are possible β like APIs, roles, or IDs.
π§± 2. Tuples β Arrays That Follow Strict Rules
Tuples define fixed length and data type order.
let user: [string, number] = ["Jhon", 22];
If you try to swap types or add another element, TypeScript will yell at you π
But you can even make them readonly:
type UserInfo = readonly [string, number];
const user1: UserInfo = ["Masayeakh", 25];
π§ Lesson:
Use tuples when you need ordered, predictable data β like
[username, id],
[lat, long],etc.
πͺ 3. Enums β Human-Readable Constant Values
Instead of using magic numbers or strings everywhere, enums make your code more readable.
enum SeatChoice {
AISLE,
MIDDLE,
WINDOW,
}
Now you can write:
let mySeat: SeatChoice = SeatChoice.MIDDLE;
You can even customize them π
enum SeatChoice2 {
AISLE = 10,
MIDDLE = 20,
WINDOW = 30,
}
π§ Lesson:
Enums make constants easier to manage, especially for configurations or fixed categories.
π‘ 4. Interfaces β The Blueprint for Objects
Interfaces tell TypeScript what structure an object should follow.
interface User {
name: string;
age: number;
isActive: boolean;
}
Now, any object claiming to be a Usermust match this shape.
You can even define functions inside:
interface MathOp {
val1: number;
val2: number;
operation(): number;
}
let result: MathOp = {
val1: 23,
val2: 32,
operation() {
return this.val1 + this.val2;
},
};
π§ Lesson:
Interfaces make code scalable β especially when working on large applications or teams.
π― Final Thoughts
Each week I learn something that makes me respect TypeScript more.
This language doesnβt just help avoid bugs β it forces me to think clearly about structure, safety, and intent.
Next up, Iβll explore Generics and Type Inference β the real brain stretchers π§ πͺ
If youβre also learning TypeScript, letβs connect and share progress!
Drop your profile or comment below π¬
Written by Masayeakh β full stack dev sharing his TypeScript journey.
Top comments (0)