Utilize utility types rather than create a new interface
Clean codeTypescriptAdd example
What to do
- Use utility types (Omit, Union, Pick ...)
Prevent problems
- Copy-Paste
- Duplication
- Readability
Explanation
Using spectific interface to avoid useless data and permit interface segregation. When we need some properties from one or several interfaces, we should use utility types.
Violations should be raised when, within the same file, there exists 'type' or 'interface' that have properties which are defined in another type or variable.
For instance:
interface User {
firstName: string;
lastName: string;
}
interface FullUser {
firstName: string;
lastName: string;
address: string;
}
is an issue since FullUser contains all properties defined in the interface User
.
Examples
Example 1:
Negative
Incorrect implementation that violates the practice.
interface User {
id: string;
firstName: string;
lastName:string;
email: string;
}
interface UserWithoutId {
firstName: string;
lastName:string;
email: string;
}
const user: UserWithoutId = {
firstName: 'John',
lastName: 'Test',
email: 'john.test@test.com'
}