You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: frontend/typescript-coding-standard.md
+45-5Lines changed: 45 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,49 @@ A good coding standard helps the code quality and development productivity becau
4
4
5
5
## 1 Introduction
6
6
7
-
There are two tools used in maitaining TypeScript coding standard: [TSLint](https://github.com/palantir/tslint) and [Prettier](https://prettier.io/). TSLint is used for code analysis and prettier is used for code format. Both tools should be configured and used in both IDE and the automatic build process.
7
+
The coding guidelines are defined in the next section. There are two tools used in maitaining TypeScript coding standard: [TSLint](https://github.com/palantir/tslint) and [Prettier](https://prettier.io/). TSLint is used for code analysis and prettier is used for code format. Both tools should be configured and used in both IDE and the automatic build process.
8
8
9
-
## 2 TS Compiler Options
9
+
## 2 Guidelines
10
+
11
+
### 2.1 Names
12
+
13
+
1. Use PascalCase for type names.
14
+
1. Do not use "I" as a prefix for interface names.
15
+
1. Use PascalCase for enum values.
16
+
1. Use camelCase for function names.
17
+
1. Use camelCase for property names and local variables.
18
+
1. Do not use `_` as a prefix for private properties.
19
+
1. Use whole words in names when possible.
20
+
21
+
### 2.2 Types
22
+
23
+
1. Do not export types/functions unless you need to share it across multiple components.
24
+
1. Do not introduce new types/values to the global namespace.
25
+
1. Shared types should be defined in a `types.ts` file or in files in `models` directory.
26
+
1. Within a file, type definitions should come first.
27
+
28
+
### 2.3 Usage
29
+
30
+
1. Use `undefined`, do't use `null`.
31
+
1. Consider objects like Nodes, Symbols, etc. as immutable outside the component that created them. Do not change them.
32
+
1. Consider arrays as immutable by default after creation.
33
+
1. More than 2 related Boolean properties on a type should be turned into an Enum flag.
34
+
1. Try to use `ts.forEach`, `ts.map`, and `ts.filter` instead of loops, i.e., no `for..in`, when it is not strongly inconvenient.
35
+
36
+
### 2.4 Comments
37
+
38
+
1. Use JSDoc style comments for functions, interfaces, enums, and classes.
39
+
40
+
### 2.5 Diagnostic Messages
41
+
42
+
1. All strings visible to the user need to be localized.
43
+
1. Use a period at the end of a sentence.
44
+
1. Use indefinite articles for indefinite entities.
45
+
1. Definite entities should be named (this is for a variable name, type name, etc..).
46
+
1. When stating a rule, the subject should be in the singular (e.g. "An external module cannot..." instead of "External modules cannot...").
47
+
1. Use present tense.
48
+
49
+
## 3 TS Compiler Options
10
50
11
51
First of all, set the following compiler options in `tsconfig.json` to enforce strict type checking and error detection.
12
52
@@ -23,7 +63,7 @@ In the above configuration, enabling `--strict` enables `--noImplicitAny`, `--no
23
63
24
64
The `skipLibCheck` tells the compiler to skil checking for all declaraton files (`*.d.ts`). There was an error in an Ionic 3.9 declaration file.
25
65
26
-
## 3 TSLint
66
+
## 4 TSLint
27
67
28
68
Both the Angular CLI tool `ng` and the Ionic CLI tool `ionic` generate a `tslint.json` file. First, install and use `tslint-angular` preset for code analyzer: `npm i -D tslint-angular`.
29
69
@@ -53,7 +93,7 @@ Then, disable code style checking rules. Update the `tslint.json` file to have t
53
93
54
94
For an Ionic project, write the npm run command as `"lint": "tslint -c tslint.json -p tsconfig.json",`. You also need to change the `ionic` generated component class names to follow the [Angular code style](https://angular.io/guide/styleguide).
55
95
56
-
## 4 Prettier
96
+
## 5 Prettier
57
97
58
98
First, install the Prettier package using `npm i -D prettier`. Then create a configuration file named `.prettierrc` that has customized rules:
59
99
@@ -71,7 +111,7 @@ First, install the Prettier package using `npm i -D prettier`. Then create a con
71
111
72
112
To make it format files automatically, set `“editor.formatOnSave”: true` in VS Code after installing the Prettier extension.
73
113
74
-
## 5 Configure Pre-commit Hooks
114
+
## 6 Configure Pre-commit Hooks
75
115
76
116
Use [`husky`](https://github.com/typicode/husky), [`pretty-quick`](https://github.com/azz/pretty-quick) and [`npm-run-all`](https://github.com/mysticatea/npm-run-all) to run Prettier on changed files before commit.
0 commit comments