Typescript Standards · TS
Guidelines for all engineers writing TypeScript.
Table of Contents
-
TypeScript Class Design Standards | TS-01
- Types
- Use type annotations for function parameters and return types | TS-01.1 |
- Use type annotations variables of complex objects | TS-01.2 |
- Use
constoverletfor variables that do not change | TS-01.3 | - Use
unknownoverany| TS-01.4 | - Use
readonlyfor properties that won’t change after initialisation | TS-01.5 | - Use
undefinedovernull| TS-01.6 | - Use optional parameters | TS-01.7 |
- Avoid redundant type annotations when TypeScript can infer types | TS-01.8 |
- Object-Oriented Programming
- Types
-
TypeScript Maintainability Standards | TS-02
- Methods do not exceed 10 statements | TS-02.1 |
- Do not duplicate code | TS-02.2 |
- Nested control statements do not exceed a depth of 2 | TS-02.3 |
- Do not allow methods with more than 4 parameters | TS-02.4 |
- Do not use magic numbers or strings | TS-02.5 |
- Use
Enums over fixed sets of constants | TS-02.6 | - Allow gaps between numeric values for enums | TS-02.7 |
- Don’t comment out code | TS-02.8 |
- Target specific versions or version ranges in
package.json| TS-02.9 | - Implement error logging for observability of problems | TS-02.10 |
- Limit the contents of a source code file to one type | TS-02.11 |
- Enable
strictNullChecksfor Type safety | TS-02.12 | - Optimise imports | TS-02.13 |
-
TypeScript Naming Standards | TS-03
- Files must follow the naming convention
{description}.{type}.{file extension}| TS-03.1 | - The type within the file name should use the conventional names | TS-03.2 |
- Symbols must use pascal case (
PascalCase) | TS-03.3 | - Symbols must avoid private property prefix | TS-03.4 |
- Components must use kebab case (
kebab-case) | TS-03.5 | - The symbol must be suffixed with the word
Component| TS-03.6 | - Component selectors should be prefixed to describe its use | TS-03.7 |
- Directives must use camel case (
camelCase) | TS-03.8 | - The symbol must be suffixed with the word
Directive| TS-03.9 | - Directive selectors should be prefixed to describe its use | TS-03.10 |
- Pipes must use camel case (
camelCase) | TS-03.11 | - The symbol must be suffixed with the word
Pipe| TS-03.12 | - Pipe selectors should be prefixed to describe their use | TS-03.13 |
- The symbol must be suffixed with the word
Module| TS-03.14 | - Modules should be named after the feature and folder that they reside in | TS-03.15 |
- Files must follow the naming convention
Enforcement
In general, the Airbnb standards are the basis for Typescript coding standards.
These are, where possible, enforced through the use of ESLint. The Audacia ESLint npm package contains some default ESLint configuration for JavaScript and TypeScript projects.