
React
Use functional components
Always use TSX functional components. Do not use defaultimport with const, because it’s harder to read and harder to import with code completion.
Props
Create the type of the props and call it(ComponentName)Props if there’s no need to export it.
Use props destructuring.
Refrain from using React.FC or React.FunctionComponent to define prop types
No Single Variable Prop Spreading in JSX Elements
Avoid using single variable prop spreading in JSX elements, like{...props}. This practice often results in code that is less readable and harder to maintain because it’s unclear which props the component is receiving.
- At a glance, it’s clearer which props the code passes down, making it easier to understand and maintain.
- It helps to prevent tight coupling between components via their props.
- Linting tools make it easier to identify misspelled or unused props when you list props explicitly.
JavaScript
Use nullish-coalescing operator ??
Use optional chaining ?.
TypeScript
Use type instead of interface
Always use type instead of interface, because they almost always overlap, and type is more flexible.
Use string literals instead of enums
String literals are the go-to way to handle enum-like values in TypeScript. They are easier to extend with Pick and Omit, and offer a better developer experience, specially with code completion. You can see why TypeScript recommends avoiding enums here.GraphQL and internal libraries
You should use enums that GraphQL codegen generates. It’s also better to use an enum when using an internal library, so the internal library doesn’t have to expose a string literal type that is not related to the internal API. Example:Styling
Use StyledComponents
Style the components with styled-components.Theming
Utilizing the theme for the majority of component styling is the preferred approach.Units of measurement
Avoid usingpx or rem values directly within the styled components. The necessary values are generally already defined in the theme, so it’s recommended to make use of the theme for these purposes.
Colors
Refrain from introducing new colors; instead, use the existing palette from the theme. Should there be a situation where the palette does not align, please leave a comment so that the team can rectify it.Enforcing No-Type Imports
Avoid type imports. To enforce this standard, an ESLint rule checks for and reports any type imports. This helps maintain consistency and readability in the TypeScript code.Why No-Type Imports
- Consistency: By avoiding type imports and using a single approach for both type and value imports, the codebase remains consistent in its module import style.
- Readability: No-type imports improve code readability by making it clear when you’re importing values or types. This reduces ambiguity and makes it easier to understand the purpose of imported symbols.
- Maintainability: It enhances codebase maintainability because developers can identify and locate type-only imports when reviewing or modifying code.
ESLint Rule
An ESLint rule,@typescript-eslint/consistent-type-imports, enforces the no-type import standard. This rule will generate errors or warnings for any type import violations.
Please note that this rule specifically addresses rare edge cases where unintentional type imports occur. TypeScript itself discourages this practice, as mentioned in the TypeScript 3.8 release notes. In most situations, you should not need to use type-only imports.
To ensure your code complies with this rule, make sure to run ESLint as part of your development workflow.