General code standards.
Following the naming conventions for JavaScript / TypeScript:
- Constant values should be upper snake case (
CONSTANT_VALUES_LIKE_THIS
) - Enums should be named in singular PascalCase with keys also in PascalCase
// Example enum with string values
enum Direction {
Up = "Up",
Down = "Down",
Left = "Left",
Right = "Right",
}
Use arrow functions with explicit return types.
// Don't do this!
// Implicit return: string
export const doSomething = (aString: string) => aString
// Explicit return: boolean
// - Picks up type error to reduce bugs
// - Builds confidence for next engineer
// - Better readability (no need to look through the fn to know what to expect)
export const doSomething2 = (aString: string): boolean => aString
// Generic function
export const doSomethingGeneric = <TOption>(option: TOption): boolean => typeof option === "string"