|
| 1 | +==================================================== |
| 2 | +📘 TypeScript: Difference Between Type and Interface |
| 3 | +==================================================== |
| 4 | + |
| 5 | +📌 1. Introduction |
| 6 | + |
| 7 | +TypeScript provides both `type` aliases and `interface` to define custom types. While they are often used interchangeably, there are key differences and best use cases. |
| 8 | + |
| 9 | +---------------------------------------------------- |
| 10 | +📌 2. Basic Definitions |
| 11 | + |
| 12 | +➡ `interface`: |
| 13 | +- Used to define the structure of an object. |
| 14 | +- Preferred for creating object shapes, especially in OOP or large-scale applications. |
| 15 | + |
| 16 | +➡ `type`: |
| 17 | +- A more flexible way to define types. |
| 18 | +- Can represent not just object shapes, but also primitives, unions, intersections, and more. |
| 19 | + |
| 20 | +---------------------------------------------------- |
| 21 | +📌 3. Syntax Examples |
| 22 | + |
| 23 | +// Using interface |
| 24 | +interface Person { |
| 25 | + name: string; |
| 26 | + age: number; |
| 27 | +} |
| 28 | + |
| 29 | +// Using type |
| 30 | +type PersonType = { |
| 31 | + name: string; |
| 32 | + age: number; |
| 33 | +}; |
| 34 | + |
| 35 | +const user1: Person = { name: "Waseem", age: 25 }; |
| 36 | +const user2: PersonType = { name: "Malik", age: 30 }; |
| 37 | + |
| 38 | +---------------------------------------------------- |
| 39 | +📌 4. Key Differences |
| 40 | + |
| 41 | +| Feature | `interface` | `type` | |
| 42 | +|----------------------------|-----------------------------------------|----------------------------------------------| |
| 43 | +| Extension | Supports `extends` | Supports intersection `&` | |
| 44 | +| Declaration Merging | ✅ Yes (can redefine/merge) | ❌ No (redefining causes error) | |
| 45 | +| Primitives & Unions | ❌ Not allowed | ✅ Supported | |
| 46 | +| Computed Properties | ❌ Limited | ✅ Fully Supported | |
| 47 | +| Readability in IDEs | ✅ More readable in intellisense | ✅ Also readable, but can get complex | |
| 48 | +| React Props (convention) | Commonly used | Also accepted | |
| 49 | +| Use in OOP | Preferred | Possible | |
| 50 | + |
| 51 | +---------------------------------------------------- |
| 52 | +📌 5. Practical Examples |
| 53 | + |
| 54 | +// Interface extension (inheritance) |
| 55 | +interface Animal { |
| 56 | + name: string; |
| 57 | +} |
| 58 | +interface Dog extends Animal { |
| 59 | + bark(): void; |
| 60 | +} |
| 61 | +const myDog: Dog = { |
| 62 | + name: "Tommy", |
| 63 | + bark: () => console.log("Woof") |
| 64 | +}; |
| 65 | + |
| 66 | +// Type intersection |
| 67 | +type AnimalType = { |
| 68 | + name: string; |
| 69 | +}; |
| 70 | +type DogType = AnimalType & { |
| 71 | + bark(): void; |
| 72 | +}; |
| 73 | +const myDog2: DogType = { |
| 74 | + name: "Rocky", |
| 75 | + bark: () => console.log("Woof!") |
| 76 | +}; |
| 77 | + |
| 78 | +// Type with union and primitives |
| 79 | +type ID = string | number; |
| 80 | +type Point = { x: number; y: number } | [number, number]; |
| 81 | + |
| 82 | +// Interface merging |
| 83 | +interface Box { |
| 84 | + height: number; |
| 85 | +} |
| 86 | +interface Box { |
| 87 | + width: number; |
| 88 | +} |
| 89 | +const myBox: Box = { height: 10, width: 20 }; |
| 90 | + |
| 91 | +// Type - cannot merge |
| 92 | +type BoxType = { |
| 93 | + height: number; |
| 94 | +}; |
| 95 | +// type BoxType = { width: number }; // ❌ Error: Duplicate identifier |
| 96 | + |
| 97 | +---------------------------------------------------- |
| 98 | +📌 6. When to Use What? |
| 99 | + |
| 100 | +✅ Use `interface` when: |
| 101 | +- You're working with object shapes or class-like structures. |
| 102 | +- You need to extend or implement in classes. |
| 103 | +- You want declaration merging. |
| 104 | + |
| 105 | +✅ Use `type` when: |
| 106 | +- You need union, intersection, or conditional types. |
| 107 | +- You're defining more complex types (like function signatures, tuples, or utility types). |
| 108 | +- You don’t need merging. |
| 109 | + |
| 110 | +---------------------------------------------------- |
| 111 | +📌 7. Interview Questions You Might Be Asked: |
| 112 | + |
| 113 | +❓ What is the difference between `type` and `interface` in TypeScript? |
| 114 | +❓ Can an interface extend a type? (Yes, using `extends`) |
| 115 | +❓ Can a type extend an interface? (Yes, using intersection `&`) |
| 116 | +❓ Can you merge interfaces? Why is that useful? |
| 117 | +❓ When would you prefer a type over an interface? |
| 118 | + |
| 119 | +---------------------------------------------------- |
| 120 | +📌 8. Advanced Tips (Bonus) |
| 121 | + |
| 122 | +✅ Mixed use: |
| 123 | +```ts |
| 124 | +interface Person { |
| 125 | + name: string; |
| 126 | +} |
| 127 | +type Employee = Person & { id: number }; |
0 commit comments