Move type vs interface

From `Formatting` to `Objects and Data Structures` section.
This commit is contained in:
Dumitru 2019-02-11 13:39:31 +02:00 committed by GitHub
parent c46f8d7599
commit b8a75392f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

118
README.md
View file

@ -1199,6 +1199,64 @@ interface Config {
**[ back to top](#table-of-contents)**
### type vs. interface
Use type when you might need a union or intersection. Use interface when you want `extends` or `implements`. There is no strict rule however, use the one that works for you.
For a more detailed explanation refer to this [answer](https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types/54101543#54101543) about the differences between `type` and `interface` in TypeScript.
**Bad:**
```ts
interface EmailConfig {
// ...
}
interface DbConfig {
// ...
}
interface Config {
// ...
}
//...
type Shape {
// ...
}
```
**Good:**
```ts
type EmailConfig {
// ...
}
type DbConfig {
// ...
}
type Config = EmailConfig | DbConfig;
// ...
interface Shape {
// ...
}
class Circle implements Shape {
// ...
}
class Square implements Shape {
// ...
}
```
**[ back to top](#table-of-contents)**
## Classes
### Classes should be small
@ -2450,64 +2508,6 @@ review.review();
**[ back to top](#table-of-contents)**
### type vs. interface
Use type when you might need a union or intersection. Use interface when you want `extends` or `implements`. There is no strict rule however, use the one that works for you.
For a more detailed explanation refer to this [answer](https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types/54101543#54101543) about the differences between `type` and `interface` in TypeScript.
**Bad:**
```ts
interface EmailConfig {
// ...
}
interface DbConfig {
// ...
}
interface Config {
// ...
}
//...
type Shape {
// ...
}
```
**Good:**
```ts
type EmailConfig {
// ...
}
type DbConfig {
// ...
}
type Config = EmailConfig | DbConfig;
// ...
interface Shape {
// ...
}
class Circle implements Shape {
// ...
}
class Square implements Shape {
// ...
}
```
**[ back to top](#table-of-contents)**
### Organize imports
With clean and easy to read import statements you can quickly see the dependencies of current code. Make sure you apply following good practices for `import` statements:
@ -2757,4 +2757,4 @@ function getActiveSubscriptions(): Promise<Subscription[]> {
}
```
**[ back to top](#table-of-contents)**
**[ back to top](#table-of-contents)**