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
+20-3Lines changed: 20 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,24 @@ A good coding standard helps the code quality and development productivity becau
6
6
7
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.
8
8
9
-
## 2 TSLint
9
+
## 2 TS Compiler Options
10
+
11
+
First of all, set the following compiler options in `tsconfig.json` to enforce strict type checking and error detection.
12
+
13
+
```json
14
+
"strict": true,
15
+
"noUnusedLocals": true,
16
+
"noUnusedParameters": true,
17
+
"noImplicitReturns": true,
18
+
"noFallthroughCasesInSwitch": true,
19
+
"skipLibCheck": true,
20
+
```
21
+
22
+
In the above configuration, enabling `--strict` enables `--noImplicitAny`, `--noImplicitThis`, `--alwaysStrict`, `--strictNullChecks`, `--strictFunctionTypes` and `--strictPropertyInitialization`.
23
+
24
+
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
+
26
+
## 3 TSLint
10
27
11
28
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`.
12
29
@@ -36,7 +53,7 @@ Then, disable code style checking rules. Update the `tslint.json` file to have t
36
53
37
54
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).
38
55
39
-
## 3 Prettier
56
+
## 4 Prettier
40
57
41
58
First, install the Prettier package using `npm i -D prettier`. Then create a configuration file named `.prettierrc` that has customized rules:
42
59
@@ -54,7 +71,7 @@ First, install the Prettier package using `npm i -D prettier`. Then create a con
54
71
55
72
To make it format files automatically, set `“editor.formatOnSave”: true` in VS Code after installing the Prettier extension.
56
73
57
-
## 4 Configure Pre-commit Hooks
74
+
## 5 Configure Pre-commit Hooks
58
75
59
76
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