- Do not use "_" as a prefix for private properties.
- Use whole words in names when possible.
- Give folders/files/components/functions unique names.
Style | Category |
---|---|
PascalCase | class / enum / decorator / component files |
camelCase | variable / parameter / function / method / property / module alias / folder names + non-component files |
CONSTANT_CASE | global constant values (declared on module level; if the value can be instantiated more than once it must use camelCase) |
- 1 file per logical component.
- Filename should match the component name.
- Use undefined. Do not use null.
- Use JSDoc-style comments (
/** JSDoc */
) for functions, interfaces, enums, and classes.- Specify all function attribute/parameter types
- Only specify function attributes/parameters with text if they are not self-explanatory.
- Use single line comments (
// line comment
) for implementation comments. - Write comments only if they add value. Do not comment for the sake of commenting.
What comments are needed so that another developer understands the code?
Keep in mind: The comments are the projects documentation. If the code is not self-explanatory, it should be refactored/commented.
- Use arrow functions over anonymous function expressions.
- Only surround arrow function parameters when necessary.
For example,(x) => x + x
is wrong but the following are correct:x => x + x
(x, y) => x + y
<T>(x: T, y: T) => x === y
- Always surround loop and conditional bodies with curly braces. Statements on the same line are allowed to omit braces.
- Open curly braces always go on the same line as whatever necessitates them.
- Use a single declaration per variable statement (i.e. use
var x = 1; var y = 2;
overvar x = 1, y = 2;
). - Separate function from the JSX if it takes more than one line (i.e. button click).
- Do not use inline styles. Use separate CSS files instead.
- Use 2 spaces for indentation.
VSCode: View -> Command Palette... -> "Indent Using Spaces" -> 2
- Imports - Prefer destructuring over importing the whole module.
- Additional variables
- Component/Class
- Optional constructor
Only use constructor if you need to set initial state or bind methods. JavaScript will automatically create an empty constructor for you.
- Definitions
- Functions
- Additional destructors
- Optional constructor
- Exports