Interfaces and type aliases both describe shapes of data in TypeScript. Choosing between them helps keep your code clear and easy to work with.
Interface vs type alias decision in Typescript
interface InterfaceName {
propertyName: type;
}
type TypeAliasName = typeExpression;Interfaces describe object shapes and can be extended or merged.
Type aliases can name any type, including primitives, unions, and tuples.
interface Person {
name: string;
age: number;
}type ID = string | number;
interface Animal {
species: string;
}
interface Dog extends Animal {
breed: string;
}type Coordinates = [number, number];
This program shows how to create an interface and a type alias with the same shape, then use them to type objects.
interface User {
id: number;
name: string;
}
type UserAlias = {
id: number;
name: string;
};
const user1: User = { id: 1, name: "Alice" };
const user2: UserAlias = { id: 2, name: "Bob" };
console.log(user1);
console.log(user2);Interfaces support declaration merging, so you can add properties later by declaring the same interface again.
Type aliases cannot be reopened once declared.
Use interfaces for objects you expect to extend or implement in classes.
Interfaces are best for describing object shapes and can be extended or merged.
Type aliases can name any type, including unions and tuples, but cannot be merged.
Choose based on whether you need extension, merging, or complex type expressions.
Practice
interface instead of a type alias in TypeScript?Solution
Step 1: Understand interface capabilities
Interfaces are designed to describe object shapes and support extension and merging.Step 2: Compare with type alias features
Type aliases can name any type but do not support merging and are less suited for extension of object shapes.Final Answer:
Useinterfacewhen you want to extend or merge object shapes. -> Option AQuick Check:
Interface = extension and merging [OK]
- Thinking interfaces can name union types
- Believing type aliases support merging
- Using interface for primitives only
Solution
Step 1: Recall type alias syntax
Type aliases use the keywordtypefollowed by the name, equals sign, and the type expression.Step 2: Identify union type declaration
The union of string literals is written as'success' | 'error'after the equals sign.Final Answer:
type Status = 'success' | 'error'; -> Option DQuick Check:
Type alias syntax = type Name = TypeExpression [OK]
- Using 'interface' for union types
- Using ':' instead of '=' in type alias
- Mixing interface and type alias syntax
console.log(user1.age);?
interface User {
name: string;
}
interface User {
age: number;
}
const user1: User = { name: 'Alice', age: 30 };Solution
Step 1: Understand interface merging
TypeScript merges multiple interface declarations with the same name into one combined interface.Step 2: Check the combined interface properties
The mergedUserinterface has bothnameandageproperties, souser1can have both.Final Answer:
30 -> Option AQuick Check:
Interface merging allows combined properties [OK]
- Expecting a duplicate identifier error
- Thinking type aliases merge
- Assuming age is undefined
type Person = {
name: string;
};
type Person = {
age: number;
};Solution
Step 1: Understand redeclaration rules for type aliases
Type aliases cannot be declared multiple times with the same name; this causes a duplicate identifier error.Step 2: Identify the fix
Renaming one of the type aliases avoids the conflict; interfaces can merge but type aliases cannot.Final Answer:
Type aliases cannot be redeclared; rename one alias. -> Option CQuick Check:
Type aliases = no redeclaration [OK]
- Assuming type aliases merge like interfaces
- Ignoring duplicate identifier error
- Trying to redeclare type alias without rename
name and age or a string ID. Which is the best approach?Solution
Step 1: Identify the need for union types
You want a type that can be either an object or a string, which requires a union type.Step 2: Choose the best syntax for union types
Type aliases can represent unions directly, while interfaces cannot represent unions themselves.Final Answer:
Use a type alias with a union of the user object type and string. -> Option BQuick Check:
Type aliases = best for unions [OK]
- Trying to use interface for union types
- Merging interfaces to represent unions
- Separating types unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
