mirror of
https://github.com/labs42io/clean-code-typescript.git
synced 2024-11-23 05:34:04 +00:00
Move type vs interface
From `Formatting` to `Objects and Data Structures` section.
This commit is contained in:
parent
c46f8d7599
commit
b8a75392f4
118
README.md
118
README.md
|
@ -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)**
|
||||
|
|
Loading…
Reference in a new issue