Skip to content

Commit 639e399

Browse files
ying.liuying.liu
authored andcommitted
add TS compiler options in code standard
1 parent a0a054d commit 639e399

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

frontend/typescript-coding-standard.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@ A good coding standard helps the code quality and development productivity becau
66

77
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.
88

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
1027

1128
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`.
1229

@@ -36,7 +53,7 @@ Then, disable code style checking rules. Update the `tslint.json` file to have t
3653

3754
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).
3855

39-
## 3 Prettier
56+
## 4 Prettier
4057

4158
First, install the Prettier package using `npm i -D prettier`. Then create a configuration file named `.prettierrc` that has customized rules:
4259

@@ -54,7 +71,7 @@ First, install the Prettier package using `npm i -D prettier`. Then create a con
5471

5572
To make it format files automatically, set `“editor.formatOnSave”: true` in VS Code after installing the Prettier extension.
5673

57-
## 4 Configure Pre-commit Hooks
74+
## 5 Configure Pre-commit Hooks
5875

5976
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.
6077

0 commit comments

Comments
 (0)