Skip to content

FlowCrypt TypeScript Style Guide

Tom J edited this page Feb 2, 2019 · 14 revisions

The following style guide is current as of January 2019.

  1. Do not in any way modify standard JS constructors such as Array, Object, etc unless you are very sure and have discussed this with @tomholub

  2. Shims are OK but generally discouraged, except when there is potential to use the benefits throughout the app

  3. When introducing new libraries, please notify & discuss with @tomholub. In particular, clear licensing is very important (MIT, Apache, BSD preferred). Please take care to find a small library with few dependencies. As a company, we are required to inform some of our customers every time we add a 3rd party library, so this cannot be taken lightly. Once a library has been selected, it should be included in lib/ as a single, UNminified file, and checked into the repo. Our repo follows a batteries-included approach when it comes to 3rd party code that we distribute with our own code.

  4. All imports should be done using ES6 import statement, except when not possible (some 3rd party libraries, in which case a html <script> tag will be used on appropriate htm pages).

  5. we use .htm instead of .html

  6. We do not use functions for looping. In particular, we never ever use .each nor $.each(). Why? Because it's hard to return from inside .each. Since .each can't be used in all cases, and consistency matters, we never use .each.

const arr = ['a', 'b', 'c'];
const obj = { bar: 1, baz: 2 };

- arr.each((x, i) => doSomething(x, 'whatnot'));
+ for(const x of arr) { // iterate over array values using OF keyword
+   doSomething(x, 'whatnot');
+ }

- arr.each((x, i) => doSomething(i, 'whatnot'));
+ for(const i in arr) { // iterate over array indexes using IN keyword
+   doSomething(i, 'whatnot');
+ }

- $.each(obj)
  1. We turn on tscode auto-format on each save. We make sure tslint is set up and functioning in our IDE.
Clone this wiki locally