TypeScript Master (50 Qs)•Part 1: Basic & Foundational TypeScript (Junior / Entry-Level)
What is the difference between `type` alias and `interface`?
junior LevelCode ExampleTypeScript
In Simple Words: Both define the shape of objects, but they have key architectural differences:
| Feature | interface | type Alias |
|---|---|---|
| Declaration Merging | Yes (multiple interfaces with same name merge automatically) | No (duplicate names throw compile error) |
| Union & Primitive Types | Cannot directly define unions (A | B) or primitives | Can define unions, tuples, primitives (type ID = string | number) |
| Extensibility | Extends via extends keyword | Extends via intersection operator (&) |
| Compiler Performance | Slightly faster cached lookups in older TS compilers | Equivalent in modern TS 5.x |
| Best Practice | Ideal for public API contracts and object schemas | Ideal for unions, utility types, and complex mapped/conditional types |
typescript
// Interface declaration merging
interface User { name: string; }
interface User { age: number; }
// User now has both: { name: string; age: number; }
// Type union (cannot be done with interface)
type Status = "pending" | "approved" | "rejected";