Skip to content

Commit 1751b6d

Browse files
ying.liuying.liu
authored andcommitted
add ts coding guidelines
1 parent dd1f907 commit 1751b6d

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

frontend/typescript-coding-standard.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,49 @@ A good coding standard helps the code quality and development productivity becau
44

55
## 1 Introduction
66

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

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
1050

1151
First of all, set the following compiler options in `tsconfig.json` to enforce strict type checking and error detection.
1252

@@ -23,7 +63,7 @@ In the above configuration, enabling `--strict` enables `--noImplicitAny`, `--no
2363

2464
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.
2565

26-
## 3 TSLint
66+
## 4 TSLint
2767

2868
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`.
2969

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

5494
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).
5595

56-
## 4 Prettier
96+
## 5 Prettier
5797

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

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

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

74-
## 5 Configure Pre-commit Hooks
114+
## 6 Configure Pre-commit Hooks
75115

76116
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.
77117

0 commit comments

Comments
 (0)