Skip to content

Commit 125cad4

Browse files
committed
write
1 parent a707f54 commit 125cad4

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed
Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
11
---
2-
title: Interfaces (+ mini project)
3-
description: description
2+
title: Interfaces
3+
description: Understand how to declare object types with the "interface" keyword, and the subtle difference interfaces have with regular type aliases.
44
menuWeight: 7.8
55
paths:
66
- switching-to-typescript/interfaces
77
---
88

99
# [](#interfaces) Interfaces
1010

11-
Interfaces just object types
11+
In the [**Using types - II**]({{@link switching_to_typescript/using_types_continued.md}}) lesson, you learned about object types and how to create them. Let's create a new custom object type using the `type` keyword that we're already familiar with.
1212

13-
can extend each other
13+
```TypeScript
14+
type Person = {
15+
name: string;
16+
age: number;
17+
hobbies: string[];
18+
};
19+
```
1420

15-
classes can extend them to add class constraints
21+
We can keep this just as it is, which would be totally okay, or we could use an [interface](https://www.typescriptlang.org/docs/handbook/typescript-tooling-in-5-minutes.html#interfaces). Interfaces and type aliases are nearly identical in their functionality; however there are two main differences:
1622

17-
## [](#mini-project) Mini-project
23+
1. Interfaces can **ONLY** hold function or object types.
24+
2. Types can only be declared once, while interfaces of the same name can be declared multiple times and will automatically merge.
25+
3. The syntax between the two differs slightly.
1826

19-
1. Make custom interface for API response
20-
2. Case interface type to API response
21-
3. Make custom interface for modified API response (with omitted key with `Omit` + extra custom key using `extends`)
22-
4. Filter the data/modify it somehow
23-
5. Return the data having the new modified response type array
27+
> When working with object types, it usually just comes down to preference whether you decide to use an interface or a type alias.
28+
29+
Using the `interface` keyword, we can easily turn our `Person` type into an interface.
30+
31+
```TypeScript
32+
// Interfaces don't need an "=" sign
33+
interface Person {
34+
name: string;
35+
age: number;
36+
hobbies: string[];
37+
};
38+
```
39+
40+
When creating a new interface, you can also use the `extends` keyword to inherit all of the object properties from another interface (or type alias):
41+
42+
```TypeScript
43+
interface Person {
44+
name: string;
45+
age: number;
46+
hobbies: string[];
47+
}
48+
49+
// This would also work just fine if "Person"
50+
// was declared with the "type" keyword instead
51+
interface Employee extends Person {
52+
occupation: string;
53+
}
54+
```
55+
56+
This is functionality is not unique to interfaces though, as it can be done with something called an [intersection type](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types) when using the `type` keyword.
57+
58+
```TypeScript
59+
type Employee = Person & {
60+
occupation: string;
61+
};
62+
```
63+
64+
Overall, the differences between interfaces and type aliases are quite subtle. In some use cases, one might be better than the other, but in general it's up to you which you want to use.
65+
66+
> To learn more about the differences between `interface` and `type`, check out [this article](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c).
67+
68+
## [](#next) Next up
69+
70+
It's finally time to [build our project]({{@link switching_to_typescript/mini_project.md}})! The project we'll be building in the next lesson will be a small API scraper utilizes many of the TypeScript features learned in the course.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Mini-project
3+
description: description
4+
menuWeight: 7.9
5+
paths:
6+
- switching-to-typescript/mini-project
7+
---
8+
9+
# [](#mini-project) Mini-project
10+
11+
1. Make custom interface for API response
12+
2. Case interface type to API response
13+
3. Make custom interface for modified API response (with omitted key with `Omit` + extra custom key using `extends`)
14+
4. Filter the data/modify it somehow
15+
5. Return the data having the new modified response type array

content/academy/switching_to_typescript/watch_mode_and_tsconfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,4 @@ And our files in **src** will automatically compile into a folder named **dist**
291291

292292
## [](#next) Next up
293293

294-
[something]({{@link switching_to_typescript/interfaces.md}})
294+
Now that we're all set up, we can move forward and start building our mini-project. But first, [let's learn]({{@link switching_to_typescript/interfaces.md}}) about the `interface` keyword!

0 commit comments

Comments
 (0)