Hey there! I wanted to share some quick thoughts on TypeScript’s interface vs type and the keyof keyword since I’ve been playing around with them lately.
So, in TypeScript, interface and type both let us define how our data looks, but they’re not the same. I think of them as different ways to keep my code organized.
-
How They’re Written: An
interfaceuses theinterfacekeyword and is usually for objects. Atypeuses thetypekeyword and can do more stuff like simple values or mixing types.- Like, here’s an
interface:interface Person { name: string; age: number; }
- And here’s a
type:type Person = { name: string; age: number; };
- Like, here’s an
-
Adding Stuff Later: With an
interface, I can add more properties later by writing it again—it just merges.typedoesn’t do that; I’d need to use&to mix them.- For
interfacemerging:interface Animal { name: string; } interface Animal { legs: number; } // Now it’s got both name and legs
- For
type:type Animal = { name: string }; type MoreAnimal = Animal & { legs: number };
- For
-
What They’re Good For: I use
interfacefor objects or when I’m working with classes.typeis more flexible—like I can do"yes" | "no"or even numbers.- Example with
type:type Status = "yes" | "no";
- Example with
-
When I Use Them: I go for
interfaceif I’m making object shapes or using classes. I picktypewhen I need to mix things up or make shortcuts.
Basically, interface is awesome for objects and growing code, but type lets me do more creative type stuff.
The keyof keyword in TypeScript is super cool—it grabs all the property names of a type and makes a list of them. It’s like a safety net to make sure I’m using the right keys when working with objects.
-
What It Does: It takes an object type and gives me its keys.
- Here’s an example:
interface User { name: string; age: number; } type UserKeys = keyof User; // It’s "name" | "age"
- Here’s an example:
-
Why I Like It: It stops me from messing up when I’m grabbing properties dynamically. I can make a function that only takes the right keys.
- Check this out:
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } const user = { name: "Alice", age: 25 }; console.log(getProperty(user, "name")); // Prints "Alice" // This would break: getProperty(user, "email"); // Error: "email" isn’t a key
- Check this out:
-
Where It’s Handy: Say I’m making a form and need to loop through fields without typos—
keyofmakes sure I don’t mess up.
In short, keyof is my go-to for keeping object stuff safe and error-free!