What is the difference between interface and type in TypeScript and when to use which?
Hi everyone! I just started learning TypeScript and came across two ways to describe object structures: `interface` and `type`. At first, I thought they were the same, but then I noticed they behave differently in some situations.
Here's an example. I can write it using `interface`:
```typescript
interface User {
name: string;
age: number;
}
```
Or using `type`:
```typescript
type User = {
name: string;
age: number;
};
```
Both options work the same when I create an object:
```typescript
const user: User = { name: 'Alex', age: 30 };
```
But then I tried to merge two types. With `interface`, I can simply write a second interface with the same name, and they will merge:
```typescript
interface User {
email: string;
}
// Now User has name,
No answers yet.
Log in to leave an answer
Blogs