|
| 1 | +# JavaScript & TypeScript |
| 2 | + |
| 3 | +## JavaScript |
| 4 | + |
| 5 | +[Sample](sample.js) |
| 6 | + |
| 7 | +- Use [TypeScript](#typescript) |
| 8 | +- Use the latest stable JavaScript syntax with a transpiler, such as [babel]. |
| 9 | +- Use [ESLint] and [Prettier] for auto-formatting and auto-fixing |
| 10 | +- Use [Jest] for unit testing |
| 11 | +- Prefer ES6 classes over prototypes. |
| 12 | +- Use strict equality checks (`===` and `!==`) except when comparing against |
| 13 | + (`null` or `undefined`). |
| 14 | +- Prefer [arrow functions] `=>`, over the `function` keyword except when |
| 15 | + defining classes or methods. |
| 16 | +- Prefer ES6 [destructuring] over object literal notation. |
| 17 | +- Use ES6 [spread] and [rest] operator wherever possible for a cleaner code. |
| 18 | +- Use `PascalCase` for classes, `lowerCamelCase` for variables and functions, |
| 19 | + `SCREAMING_SNAKE_CASE` for constants, `_singleLeadingUnderscore` for private |
| 20 | + variables and functions. |
| 21 | +- Prefer [template strings] over string concatenation. |
| 22 | +- Prefer promises over callbacks. |
| 23 | +- Prefer array functions like `forEach`, `map`, `filter` and `reduce` over `for/while` loops. |
| 24 | +- Use `const` for declaring variables that will never be re-assigned, and `let` |
| 25 | + otherwise. |
| 26 | +- Avoid `var` to declare variables. |
| 27 | +- Prefer [async/await] over traditional promise syntax |
| 28 | +- Use the [Nullish coalescing operator] `??` |
| 29 | + |
| 30 | +## TypeScript |
| 31 | + |
| 32 | +- Use TypeScript in [strict mode] |
| 33 | +- Prefer [Functions] over [Classes] |
| 34 | +- Use `PascalCase` for [Interfaces] and [Type Aliases] |
| 35 | +- Use [readonly] properties where applicable |
| 36 | +- Use [const Assertions] where applicable to avoid type widening |
| 37 | +- Avoid [Mixins] |
| 38 | +- Avoid [Decorators] |
| 39 | +- Avoid [Overloading Functions] |
| 40 | +- Prefer [Optional Properties] in an interface rather than declaring the |
| 41 | + property type as `T | undefined` |
| 42 | +- Prefer explicitly defining interfaces over [Extending Interfaces] |
| 43 | +- Avoid the use of the [any] type |
| 44 | +- Avoid the [Non-null assertion operator] |
| 45 | +- Avoid [Type Assertions] |
| 46 | +- Prefer the `as`-syntax for [Type Assertions] over the angle-bracket syntax |
| 47 | +- Prefer [Type Guards] over [Type Assertions] |
| 48 | +- Prefer [Union Types], [Lookup Types], [Mapped Types] and [const Assertions] |
| 49 | + over [Enums] |
| 50 | +- Prefer [arrow functions] `=>`, over the `function` keyword except when using |
| 51 | + [Generics] |
| 52 | + |
| 53 | +## Formatting |
| 54 | + |
| 55 | +- Use [Prettier defaults](https://prettier.io/docs/en/options.html) with the following additional configuration (.prettierrc): |
| 56 | + |
| 57 | + ```json |
| 58 | + { |
| 59 | + "singleQuote": true |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | + This configuration includes: |
| 64 | + - Use semicolons at the end of each statement ([sample](/javascript/sample.js#L5)) |
| 65 | + - Prefer single quotes ([sample](/javascript/sample.js#L11)) |
| 66 | + - Use a trailing comma after each item in a multi-line array or object literal, including the last item. ([sample](/javascript/sample.js#L11)) |
| 67 | + |
| 68 | +If ESLint is used along with Prettier, the ESLInt plugin [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) should also be used to turn off all ESLint style rules that are already handled by Prettier. |
| 69 | + |
| 70 | +[babel]: https://babeljs.io/ |
| 71 | +[eslint]: https://eslint.org/ |
| 72 | +[prettier]: https://prettier.io/ |
| 73 | +[jest]: /testing-jest/ |
| 74 | +[template strings]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings |
| 75 | +[arrow functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions |
| 76 | +[destructuring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment |
| 77 | +[spread]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax |
| 78 | +[rest]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters |
| 79 | +[functions]: https://www.typescriptlang.org/docs/handbook/2/functions.html |
| 80 | +[classes]: https://www.typescriptlang.org/docs/handbook/2/classes.html |
| 81 | +[readonly]: https://www.typescriptlang.org/docs/handbook/2/objects.html#readonly-properties |
| 82 | +[const Assertions]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions |
| 83 | +[overloading functions]: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads |
| 84 | +[async/await]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await |
| 85 | +[optional properties]: https://www.typescriptlang.org/docs/handbook/2/objects.html#optional-properties |
| 86 | +[extending interfaces]: https://www.typescriptlang.org/docs/handbook/2/objects.html#extending-types |
| 87 | +[any]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any |
| 88 | +[non-null assertion operator]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator |
| 89 | +[type assertions]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions |
| 90 | +[Nullish coalescing operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing |
| 91 | +[Type Guards]: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#typeof-type-guards |
| 92 | +[generics]: https://www.typescriptlang.org/docs/handbook/2/generics.html |
| 93 | +[strict mode]: https://www.typescriptlang.org/tsconfig/#strict |
| 94 | +[mixins]: https://www.typescriptlang.org/docs/handbook/mixins.html |
| 95 | +[decorators]: https://www.typescriptlang.org/docs/handbook/decorators.html |
| 96 | +[union types]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types |
| 97 | +[lookup types]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types |
| 98 | +[mapped types]: https://www.typescriptlang.org/docs/handbook/2/mapped-types.html |
| 99 | +[enums]: https://www.typescriptlang.org/docs/handbook/enums.html |
| 100 | +[interfaces]: https://www.typescriptlang.org/docs/handbook/2/objects.html |
| 101 | +[type aliases]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases |
0 commit comments