-
Notifications
You must be signed in to change notification settings - Fork 9
feat: typescript-guidelines #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
||
## Don't repeat yourself | ||
|
||
Never repeat types, use and abuse [generics](https://www.typescriptlang.org/docs/handbook/2/generics.html) and [utility types](https://www.typescriptlang.org/docs/handbook/utility-types.html) to derive them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about splitting the examples into at least two subsections to better illustrate each concept?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reordered the examples and added some explanation to each line. I don't fully understand what you mean; in that section, it doesn't make sense to split the examples since they follow the same code flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very complete @karlos1337. Thank you for starting this! 😄 I still need to read carefully some of the sections and think about some of the points. Will do in the following weeks.
For now there are 3 basic things that we should cover from the start:
- State that we prefer types instead of interfaces.
- Our philosophy regarding implicit / explicit return types.
- Named types vs anonymous types
I remember we used to have different opinions on the second statement. What's your opinion now? Perhaps we could discuss it.
Regarding the third statement, this is particularly relevant in React, where we use props as input parameters. I would like us to be consistent across projects. I don't have a strong opinion, but I tend to prefer anonymous types when they don't need to be reused, as it can sometimes be challenging to come up with the best names for those types, or they may add unnecessary boilerplate.
I don't have a strong opinion, but I'd say types are more usable and readable than interfaces. It's not a huge difference anyway; you could achieve the same with both.
My problem with this is not really about implicit vs. explicit; it's more about returning implicit and not being conscious about what you are returning. Most of the time, that implies returning a very complex type inferred from your logic, which, later on, could be a problem to handle or maintain. Using explicit, you are fully conscious of what you are returning, resulting in better code flow.
I prefer named types and always export them. Other cases imply hiding those types and, in case you need them (because it's better to derive than repeat), forcing you to unwrap (nightmare) or invent a folder structure to move every shared type there with weird organization (or no organization at all).
IMO that's never unnecessary. Creating types from bottom to top will make component props types straightforward, and 99% of the time, I think that names will be obvious export type MyComponentProps = {
user?: User,
productsCount?: number
};
export const MyComponent ({ user = {}, productsCount = 0 }: MyComponentProps) => {
...
} |
cd299da
to
a8dc12e
Compare
a8dc12e
to
a123d41
Compare
Reason for change
Adds typescript guidelines.
List of Changes
Checklist