|
1 | 1 | ---
|
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. |
4 | 4 | menuWeight: 7.8
|
5 | 5 | paths:
|
6 | 6 | - switching-to-typescript/interfaces
|
7 | 7 | ---
|
8 | 8 |
|
9 | 9 | # [](#interfaces) Interfaces
|
10 | 10 |
|
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. |
12 | 12 |
|
13 |
| -can extend each other |
| 13 | +```TypeScript |
| 14 | +type Person = { |
| 15 | + name: string; |
| 16 | + age: number; |
| 17 | + hobbies: string[]; |
| 18 | +}; |
| 19 | +``` |
14 | 20 |
|
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: |
16 | 22 |
|
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. |
18 | 26 |
|
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. |
0 commit comments