Skip to content

Coding Guidelines

Erich Gamma edited this page Nov 17, 2015 · 23 revisions

While these guidelines are mainly meant for contributors to the TypeScript project, certain suggestions apply as to how idiomatic TypeScript code should be written (e.g. the "Names" and "Style" sections).

Names

  1. Use PascalCase for type names.
  2. Use PascalCase for enum values.
  3. Use camelCase for function names.
  4. Use camelCase for property names and local variables.
  5. Use whole words in names when possible.

Types

  1. Do not export types/functions unless you need to share it across multiple components.
  2. Do not introduce new types/values to the global namespace.

Comments

  1. Use JSDoc style comments for functions, interfaces, enums, and classes.

Strings

  1. Use double quotes for strings shown to the user that need to be externalized.
  2. Use single quotes otherwise.
  3. All strings visible to the user need to be externalized.

Style

  1. Use arrow functions over anonymous function expressions.
  2. Only surround arrow function parameters when necessary.
    For example, (x) => x + x is wrong but the following are correct:
  3. x => x + x
  4. (x,y) => x + y
  5. <T>(x: T, y: T) => x === y
  6. Always surround loop and conditional bodies with curly braces.
  7. Open curly braces always go on the same line as whatever necessitates them.
  8. Parenthesized constructs should have no surrounding whitespace.
    A single space follows commas, colons, and semicolons in those constructs. For example:
  9. for (var i = 0, n = str.length; i < 10; i++) { }
  10. if (x < 10) { }
  11. function f(x: number, y: string): void { }
Clone this wiki locally