Skip to content

ihr9191/ass1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

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.

Differences Between Interfaces and Types in TypeScript

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 interface uses the interface keyword and is usually for objects. A type uses the type keyword 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;
      };
  • Adding Stuff Later: With an interface, I can add more properties later by writing it again—it just merges. type doesn’t do that; I’d need to use & to mix them.

    • For interface merging:
      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 };
  • What They’re Good For: I use interface for objects or when I’m working with classes. type is more flexible—like I can do "yes" | "no" or even numbers.

    • Example with type:
      type Status = "yes" | "no";
  • When I Use Them: I go for interface if I’m making object shapes or using classes. I pick type when 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.

What’s the keyof Keyword in TypeScript? With an Example

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"
  • 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
  • Where It’s Handy: Say I’m making a form and need to loop through fields without typos—keyof makes sure I don’t mess up.

In short, keyof is my go-to for keeping object stuff safe and error-free!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published