Closed matvaleriano closed 2 years ago
@matvaleriano while your point is a valid improvement and a Typescript syntactic sugar to reference field types, it is not necessarily a clean code improvement.
From the perspective of maintaining the code clean and readable, both examples are good. While using id: Person['id']
has some advantages when doing refactorings, the id: string
wouldn't compile as well if you change the type.
I saw in many interviews people that doesn't know that we can access
Type['attribute']
like this. In some cases it's good because typescript will show you where code will break. For examplegiven we have a type Person
many people create specific functions like this
besides if you change the attribute's type or name it'll not break anything and you'll suffer to find every place to update, but when we use like this below
typescript will tell you where code will break.