Skip to content

Code style guide

TL;DR

Use comments to explain code that's hard to understand. Use JSDoc comments to explain functions' purposes, their parameters, and their return types (except if they don't return anything). Always annotate variables, function parameters, and function return types, unless they violate @typescript-eslint/no-inferrable-types.

  • Use comments where necessary.
    • Don't write unnecessary comments like this:
      // This is a function
      function func() {
      
    • Write comments where a line's intention cannot be clearly understood, like this:
      // Explain what the complex logic does
      if (/* complex logic */) {
      
    • Write JSDoc comments at the top of functions, like this:
      /**
       * Does something.
       * @param param1 A parameter.
       */
      function func(param1: string): void {
      
      • Don't add @returns comments to functions that don't return values.
      • Don't annotate the types of values that already have their types explicitly declared (or implicitly inferred)
  • Always annotate variables, parameters, and return types.

    // Don't do this!
    const str: string = "";
    const str1 = someFunction();
    function func(param) {
      return;
    }
    
    // Do this!
    const str = "";
    const str1: string = someFunction();
    function func(param: string): void {
      return;
    }
    

Last update: April 18, 2022
Created: April 18, 2022
Back to top