Generic
ELI5 — The Vibe Check
A generic is like a recipe that works for any ingredient. Instead of writing a 'sort apples' function AND a 'sort oranges' function, you write one 'sort anything' function and just tell it what you're sorting when you use it.
Real Talk
Generics allow you to write reusable code that works with multiple types while preserving type safety. You define a function or class with a type parameter (like T) that gets filled in at usage time, avoiding the need for duplicated type-specific implementations.
Show Me The Code
// TypeScript generic function
function first<T>(arr: T[]): T {
return arr[0];
}
first<number>([1, 2, 3]); // returns number
first<string>(["a", "b"]); // returns string
When You'll Hear This
"Make that utility function generic so it works with any type." / "The API returns a generic response wrapper."
Related Terms
Dynamic Typing
Dynamic typing is like a box that can hold anything — toys, food, or a cat — and you don't have to label what goes in it ahead of time.
Interface
An interface is like a job description. It says 'whatever fills this role must be able to do X, Y, and Z' without caring how they do it.
OOP (OOP)
OOP is a way of organising code by modelling the world as objects — things that have properties (what they are) and methods (what they do).
Type Inference
Type inference is the compiler being smart enough to figure out what type something is without you having to spell it out.