代码仓库初始化模板 (5) -- Typescript

目录

Typescript 强类型 Javascript 超集。

安装

1
npm install --save-dev typescript tsc-alias
1
npm install --save-dev ts-node tsconfig-paths

配置

在代码仓库根目录下添加文件:

tsconfig.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
"include": ["src/**/*"],
"compilerOptions": {
// Type Checking
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strict": true,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,

// Modules
"module": "CommonJS",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"]
},
"resolveJsonModule": true,
"rootDir": ".",

// Emit
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "dist",

// JavaScript Support
"allowJs": true,

// Editor Support

// Interop Constraints
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,

// Backwards

// Language and Environment
"target": "es6",

// Compiler Diagnostics
"diagnostics": true

// Projects

// Output Formatting

// Completeness

// Command Line

// Watch Options
},
"watchOptions": {},
"typeAcquisition": {}
}

在 tsconfig.json 中添加配置:

1
2
3
4
5
{
"ts-node": {
"require": ["tsconfig-paths/register"]
}
}

在 tsconfig.json 中添加配置:

1
2
3
{
"references": [{ "path": "./tsconfig.web.json" }]
}
tsconfig.web.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
"include": ["src/**/*"],
"compilerOptions": {
// Type Checking
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strict": true,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,

// Modules
"module": "ESNext",
"moduleResolution": "Node",
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"]
},
"resolveJsonModule": true,
"rootDir": ".",

// Emit
"sourceMap": true,

// JavaScript Support
"allowJs": true,

// Editor Support

// Interop Constraints

// Backwards

// Language and Environment

// Compiler Diagnostics

// Projects
"composite": true
}
}

编译

1
tsc --project tsconfig.json && tsc-alias --project tsconfig.json
1
ts-node --project tsconfig.json build/build.dev.ts