diff --git a/README.md b/README.md index a7a712d..91c5ca3 100644 --- a/README.md +++ b/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 { } ``` -**[⬆ back to top](#table-of-contents)** \ No newline at end of file +**[⬆ back to top](#table-of-contents)**