# TypeScript Express 環境、建置 (tsc、tslint、prettier、ts-node)
### Express 專案上使用
https://khalilstemmler.com/blogs/typescript/node-starter-project/
***Install Setup***
```
npm init -y
npm install typescript --save-dev
npm install @types/node
npm install --save-dev ts-node nodemon
npm install --save-dev rimraf
```
```
yarn add -D typescript
```
***Create*** `tsconfig.json`
```json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6"],
"allowJs": true,
"outDir": "dist",
"strict": false,
},
"include": ["src"]
}
```
***Create ts*** `src/index.ts`
```typescript
// src/index.ts
function squareOf(n: number): number {
return n * n
}
squareOf(6)
squareOf("7")
// error TS2345: Argument of type '"6"' is not assignable to parameter of type 'number'.
```
***nodemon***
```json
// nodemon.json
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "ts-node ./src/index.ts"
}
```
***package*** 設定
```json
"scripts": {
"dev": "nodemon",
"build": "rimraf ./dist && tsc",
"start": "npm run build && node dist/index.js"
},
```
`npm run dev` - 開發
`npm run build` - 移除舊的 dist 輸出新的 dist
***vscode prettier***
```json
// vscode/setting
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
```
```json
.prettierrc
{
"arrowParens": "always",
"bracketSpacing": false,
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 120,
"proseWrap": "always",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false,
"vueIndentScriptAndStyle": false
}
```
***vscode tslint***
```json
// tslint.json
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {},
"rules": {
"semicolon": false,
"trailing-comma": false
},
"rulesDirectory": []
}
```