A Tuple is a special array with a fixed number of elements and fixed types in order.
// This is just a normal array (no type restrictions)
let tUser = ["hc", 131, true];Since you didn't specify types, TypeScript infers it as:
(string | number | boolean)[]let rgb: [number, number, number] = [255, 255, 255];This ensures:
- Only 3 items
- Each must be a number
type MyUser = [number, string]; // Tuple: first number, second string
const newUser12: MyUser = [112, "an"];Tuples are arrays internally, so they inherit array methods.
newUser12.push(1); // Allowed by TS (not desirable)
newUser12.push("2"); // Still allowedIf you want to prevent push, make the tuple readonly:
type MyUser = readonly [number, string];Now:
newUser12.push(1); // ❌ ErrornewUser12.map(value => console.log(value));Enums represent a fixed set of named constants.
const AISLE = 0;
const MIDDLE = 1;
const WINDOW = 2;
if (seat === AISLE) { }This works, but is not descriptive.
enum SeatChoice {
AISLE,
MIDDLE,
WINDOW
}
const hcSeat = SeatChoice.AISLE; // hcSeat = 0 (default numbering)enum MySeatChoice {
AISLE = 10,
MIDDLE = 20,
WINDOW = 30
}enum NewSeatChoice {
AISLE = "aisle",
MIDDLE = 20,
WINDOW = 30
}const enum is optimized by the compiler and does not exist at runtime.
It replaces enum usage with literal values → faster & smaller code.
const enum NewSeatChoice1 {
AISLE = "aisle",
MIDDLE = 20,
WINDOW = 30
}
const mySeat = NewSeatChoice1.AISLE; // Compiled to "aisle"| Concept | What it Means | Key Point |
|---|---|---|
| Tuple | Fixed-length typed array | Can lock order & type of items |
| Normal Array | Dynamic length | All values share a general type |
| Enum | Named constant values | Improves readability & safety |
const enum |
Compile-time optimized enum | Removes runtime overhead |
If you'd like, I can now convert this entire set into a printable one-page cheat sheet PDF — just tell me:
"Make PDF" 👈