vscode配置typescript和eslint的环境

terrylin terrylin     2023-03-21     249

关键词:

 

一、typescript配置

tsconfig.build.json

  "extends": "./tsconfig.json",
  "compilerOptions": 
    "outDir": "./deploy/dist",
  ,
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]

 

tsconfig.json

  "compilerOptions": 
    "lib": ["es5", "es6"],
    "target": "es6",
    "module": "commonjs",
    "baseUrl": ".",
    "paths": 
        "@common/*":["src/common/*"],
        "@configuration/*":["src/modules/configuration"],
        "@creative/*": ["src/modules/creative/*"],
        "@admin/*": ["src/modules/admin/*"],
        "@auth/*":["src/modules/auth/*"],
    ,
    // 允许编译javascript文件
    "allowJs": true,
    // 报告.js文件中的错误。与allowJs一起使用
    "checkJs": true,
    // "plugins": [],
    // 若有未使用的局部变量则抛错
    "noUnusedLocals": false,
    // 若有未使用的参数则抛错
    "noUnusedParameters": false,
    // 类型为any时,是否需要发出警告,设置true,则不警告
    "noImplicitAny": false,
    // 提供迭代器全面支持
    "downlevelIteration": true,
    // 去掉注解
    "removeComments": true,
    // 从tslib导入外部的辅助方法
    "importHelpers": true,
    // 遇到@internal注解时,不会触发代码定义
    "stripInternal": true,
    // 错误信息,跟踪信息将带有颜色和样式
    "pretty": true,
    // 如果不是函数中的所有路径都有返回值,则提示Error
    "noImplicitReturns": true,
    // 允许从没有设置默认导出的模块中默认导入
    "allowSyntheticDefaultImports": true,
    // 使用元数据特性
    "emitDecoratorMetadata": true,
    // 支持ES7的装饰器特性
    "experimentalDecorators": true,
    // 将严格校验switch-case语法
    "noFallthroughCasesInSwitch": true,
    // 严格null检查模式,null和undefined值不包含在任何类型里
    "strictNullChecks": true,
    // 保存上一次的编译信息,下一次进行增量更新
    "incremental": false,
    // 不生成定义文件d.ts
    "declaration": false,
    // 生成.map文件
    "sourceMap": false,
    // 跳过默认库检查
    "skipLibCheck": true,
    // 输出文件的根目录
    "outDir": "./dist",
    // 模块的解析策略
    // "moduleResolution": "node",
  ,
  "include": ["src/**/*", "test/**/*"],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]

 

二、lint配置

.eslintrc

  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "standard",
    "prettier/@typescript-eslint",
    "prettier"
  ],
  "parserOptions": 
    "project": "./tsconfig.json"
  ,
  "rules": 
    "no-useless-constructor": "off",
    "@typescript-eslint/indent": ["error", 4,  "VariableDeclarator": 4, "SwitchCase": 1 ],
    "@typescript-eslint/no-unused-vars": ["error", 
        "vars": "all",
        "args": "none",
        "ignoreRestSiblings": true
      ],
    "@typescript-eslint/explicit-member-accessibility": ["error", "accessibility": "no-public"],
    "@typescript-eslint/explicit-function-return-type": ["off",
      
        "allowExpressions": true,
        "allowTypedFunctionExpressions": true
      ],
    "@typescript-eslint/interface-name-prefix": 0,
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-use-before-define": "off",
    "@typescript-eslint/no-parameter-properties": 0,
    "@typescript-eslint/camelcase": ["off", "properties": "always"],
    "no-console": ["warn",  "allow": ["warn", "error"] ],
    "eqeqeq": ["warn", "always"]
  ,
  "env": 
    "node": true,
    "es6": true,
    "mocha": true,
    "jest": true
  

tslint.json

  "defaultSeverity": "error",
  "extends": ["tslint:recommended", "tslint-config-prettier"],
  "jsRules": 
    "no-unused-expression": true
  ,
  "rules": 
    "eofline": false,
    "quotemark": [true, "single"],
    "indent": false,
    "member-access": [false],
    "ordered-imports": [false],
    "max-line-length": [true, 450],
    "member-ordering": [false],
    "curly": false,
    "interface-name": [false],
    "array-type": [false],
    "no-empty-interface": false,
    "no-empty": false,
    "arrow-parens": false,
    "object-literal-sort-keys": false,
    "no-unused-expression": false,
    "max-classes-per-file": false,
    "variable-name": [false],
    "one-line": [false],
    "one-variable-per-declaration": [false],
    "semicolon": [true, "always", "ignore-bound-class-methods"],
    "no-console": [false],
    "space-before-function-paren": false,
    "no-shadowed-variable": false
  ,
  "rulesDirectory": []

.prettierrc.js  

module.exports = 
    // 一行最多 100 字符
    printWidth: 400,
    // 使用 4 个空格缩进
    tabWidth: 4,
    // 不使用缩进符,而使用空格
    useTabs: false,
    // 行尾需要有分号
    semi: true,
    // 使用单引号
    singleQuote: true,
    // 末尾不需要逗号
    trailingComma: "es5",
    // 大括号内的首尾需要空格
    bracketSpacing: true,
    // 箭头函数,只有一个参数的时候,也需要括号
    arrowParens: "always",
    parser: ‘typescript‘
;

  

 

相关依赖

"scripts": 
    "build": "rimraf dist && tsc -p tsconfig.json",
    "format": "prettier --write "src/**/*.ts" "test/**/*.ts"",
    "start": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "rimraf dist && cross-env NODE_ENV=development concurrently --handle-input "wait-on dist/main.js && nodemon" "tsc -w -p tsconfig.build.json" ",
    "start:debug": "nodemon --config nodemon-debug.json",
    "start:dist": "node dist/main.js",
    "pm2:prd": "pm2 start ./bin/app.config.js --env production",
    "pm2:preprd": "pm2 start ./bin/app.config.js --env preproduction",
    "pm2:deve": "pm2 start ./bin/app.config.js --env development",
    "pm2:stop": "pm2 stop youtu-service",
    "pm2:restart": "pm2 restart youtu-service",
    "pm2:delete": "pm2 delete youtu-service",
    "lint": "eslint ‘src/**/*.ts,js‘ --fix",
    "lint:ts": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "docs": "compodoc -p tsconfig.json -s"
  ,
"devDependencies": 
    "@nestjs/testing": "^6.3.2",
    "@types/es6-shim": "^0.31.39",
    "@types/express": "^4.17.0",
    "@types/jest": "^24.0.15",
    "@types/lodash": "^4.14.135",
    "@types/node": "^12.0.10",
    "@types/supertest": "^2.0.7",
    "@typescript-eslint/eslint-plugin": "^1.11.0",
    "@typescript-eslint/parser": "^1.11.0",
    "concurrently": "^4.1.1",
    "eslint": "^6.0.1",
    "eslint-config-prettier": "^6.0.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.18.0",
    "eslint-plugin-node": "^9.1.0",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "jest": "^24.8.0",
    "nodemon": "^1.19.1",
    "prettier": "^1.18.2",
    "rimraf": "^2.6.3",
    "supertest": "^3.4.2",
    "ts-jest": "^23.10.5",
    "ts-loader": "^4.5.0",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.8.0",
    "tslint": "^5.11.0",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^3.7.3",
    "wait-on": "^3.2.0"
  ,
  "engines": 
    "node": ">=8.9.0"
  ,
  "jest": 
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": 
      "^.+\.(t|j)s$": "ts-jest"
    ,
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  

  

 

vscode配置eslint规则和自动修复(代码片段)

VSCODE配置eslint规则和自动修复vscode安装以下插件EsLint、vetur、Prettier-Codeformattervscode设置了添加配置项,,默认会去查找你项目中的eslint配置文件"workbench.iconTheme":"material-icon-theme","explorer.confirmDragAndD 查看详情

vscode自动eslint校验

...些弯路,这里记录一下。如果用ts,eslint.validate里再加入typescript``typescriptreact如此,校验修复的事情不再让prettier和vetur来插手,纯正的eslint规则。(想要prettier的话其实作为eslint的插件来引入使用会更方便)同样的vscode,同样的s... 查看详情

VS Code + ESLint + Prettier + Google Style + Typescript

】VSCode+ESLint+Prettier+GoogleStyle+Typescript【英文标题】:【发布时间】:2019-11-1610:51:51【问题描述】:我正在努力实现这一目标:使用VSCode作为我的JavaScript和TypeScript编辑器,在保存JavaScript/TypeScript文档时自动应用来自eslint-config-googl... 查看详情

Yarn 3.x PNP typescript CRA eslint 不适用于 VSCode

】Yarn3.xPNPtypescriptCRAeslint不适用于VSCode【英文标题】:Yarn3.xPNPtypescriptCRAeslintnotworkingwithVSCode【发布时间】:2021-12-0407:55:23【问题描述】:我已经尝试了9个小时。我已经浏览了我可以在互联网上找到的每一个github问题、博客、链... 查看详情

vscode配置文件

...matter.html":"js-beautify-html","vetur.format.defaultFormatter.js":"vscode-typescript","vetur.format.defaultFormatterOptions":"js-beautify-html":"wrap_attributes":"force-expand-multiline","prettyhtml":"printWidth":100,"singleQuote":false,//workbench"workbench.colorTheme":"Monokai","workbench.startup... 查看详情

Vue/TypeScript/ESLint/Prettier/Vetur 格式不起作用

】Vue/TypeScript/ESLint/Prettier/Vetur格式不起作用【英文标题】:Vue/TypeScript/ESLint/Prettier/Veturformattingdoesn\'twork【发布时间】:2021-10-0709:30:41【问题描述】:试图在VSCode中获得Vue/TypeScript/ESLint/Prettier/Vetur格式是一场噩梦。在这方面有很... 查看详情

配置 ESLint 以将 .ts 和 .tsx 解析为 Typescript,将 .js 和 .jsx 解析为 Ecmascript

】配置ESLint以将.ts和.tsx解析为Typescript,将.js和.jsx解析为Ecmascript【英文标题】:ConfigureESLinttoparse.tsand.tsxasTypescriptand.jsand.jsxasEcmascript【发布时间】:2020-11-0705:02:20【问题描述】:我已经安装了typescript以在我的CreateReactApp项目中... 查看详情

仅为 TS/TSX 文件删除 CR - VSCode 1.46 上的 Prettier ESLint

...间】:2020-11-2105:38:25【问题描述】:我使用CreateReactApps的Typescript模板创建了一个React项目,为ESLint6.8.0添加了必要的插件,并将ESLint和prettier配置在一 查看详情

ESLint 无法识别“@typescript-eslint/eslint-plugin”

】ESLint无法识别“@typescript-eslint/eslint-plugin”【英文标题】:ESLintisnotrecognizing"@typescript-eslint/eslint-plugin"【发布时间】:2020-04-0419:44:35【问题描述】:我一直试图让ESLint在VSCode中处理一个新的Angular项目,但它无法加载“@ty... 查看详情

ESLint 无法识别根别名 (@)

...523:13:45【问题描述】:我有一个相当多的VueCLI应用程序(Typescript+AirbnbESLint配置),ESLint无法解析我的根别名(@),但编译器和vscode可以轻松解决它。(见下文)。我有另一个具有类似设置并使用完全相同的机器的项目,ESLint可以... 查看详情

vscode中eslint的配置

...代码的呢???这不是废话吗,肯定是有的呀。 打开vscode,搜索插件eslint 安装插件 安装好了重新打开项目就能用了哦 随便打开一个文件,调整代码如下如果不符合之前配置eslint规则的,就会出现红色波浪线,鼠... 查看详情

vscode配置eslint,保存格式化样式

1-vscode中安装eslint2-在vue项目中开启esLint检测,在vue.config.js文件中lintOnSave:true,3-下面是我vscode的配置文件settings.json文件{//VScode主题配置"editor.tabSize":2,"eslint.autoFixOnSave":true,//每次保存的时候将代码按eslint格式进行修复"editor.lineH 查看详情

使用 airbnb 和 prettier 扩展的 ESLint 配置,用于使用 typescript、jest 和 react-hooks 的 react 项目

】使用airbnb和prettier扩展的ESLint配置,用于使用typescript、jest和react-hooks的react项目【英文标题】:ESLintconfigextendedwithairbnbandprettier,forareactprojectusingtypescript,jestandreact-hooks【发布时间】:2019-11-0219:33:48【问题描述】:我很困惑如何... 查看详情

使用 Prettier Eslint 时找不到模块“@typescript-eslint/parser”

】使用PrettierEslint时找不到模块“@typescript-eslint/parser”【英文标题】:Cannotfindmodule\'@typescript-eslint/parser\'whenusingPrettierEslint【发布时间】:2021-06-0107:15:25【问题描述】:保存文件时,我在VSCode上的PrettierEslint输出中收到以下错误... 查看详情

vscode配置eslint自动保存功能不生效

...决,后来终于解决了,在此总结,把一些坑分享出去。1.vscode扩展中安装eslint 2.eslint扩展要是开启状态(开启关闭如果没生效,重启vscode即可)3.修改eslint的setting(配置),步骤如下 参考我的配置,可以直接复制过去:"style... 查看详情

如何使用 ESLint + Prettier + Airbnb 规则 + TypeScript + Vetur 配置 Vue CLI 4?

】如何使用ESLint+Prettier+Airbnb规则+TypeScript+Vetur配置VueCLI4?【英文标题】:HowtoconfigureVueCLI4withESLint+Prettier+Airbnbrules+TypeScript+Vetur?【发布时间】:2020-02-2810:29:03【问题描述】:当使用VueCLIv4.0.5创建一个新项目并检查TypeScript和Linter/Fo... 查看详情

在vscode中配置代码自动eslint格式化(修改eslint规则eslint忽略文件)(代码片段)

...slint依赖Nuxt项目(vue项目应该相同),在使用VScode进行开发时,如何配置VScode在保存时,自动eslint格式化,并修改部分eslint规则,使其不与自动格式化的规则相冲突。这样可以大大提升我们的开发效率&... 查看详情

vscode中eslint插件的配置

用vue-cli构建vue项目,会有个eslint代码检测的安装可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f)这时候要在vscode上装一个eslint插件装完后在文件-----》首选项-------》设置里找到settings.json(快捷键忘了) ... 查看详情