[muzzik 分享]:typescript-eslint 太香了

eslint.zip (4.4 KB)
这是公司项目的,解压到项目根目录,另外除了需要 npm i,还要记得在 vscode 内安装 eslint 插件,仅供参考

太感谢了 :yum:

基本每年都会尝试使用eslint,但是每次都放弃了
应该是缺了这个文章的原因,下一个项目搞起

typescript-eslint 都有默认配置的,推荐打勾的就是默认的,我现在就是先看一遍所有规则配置,把部分自己刚需的禁用掉,然后再去看那些没推荐的,好的就用上

1赞

有用的几个 rule 配置

		// 注释首尾必须有空格
		"spaced-comment": [
			"error",
			"always",
			{
				"line": {
					"markers": ["/"],
					"exceptions": ["-", "+"]
				},
				"block": {
					"markers": ["!"],
					"exceptions": ["*"],
					"balanced": true
				}
			}
		],
		// 关闭不允许使用 any
		"@typescript-eslint/no-explicit-any": "off",
		// 一致的类型断言 使用 as type 而非 <type>
		"@typescript-eslint/consistent-type-assertions": [
			"error",
			{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }
		],
		// 必须存在返回类型
		"@typescript-eslint/explicit-function-return-type": "error",
		// 类成员换行(单行类成员则忽略)
		"@typescript-eslint/lines-between-class-members": [
			"error",
			"always",
			{ "exceptAfterSingleLine": true }
		],
		// 换行规则 ("const", "let", "var", "block")前后换行
		"@typescript-eslint/padding-line-between-statements": [
			"error",
			{ "blankLine": "always", "prev": ["const", "let", "var", "block"], "next": "*" },
			{
				"blankLine": "any",
				"prev": ["const", "let", "var"],
				"next": ["const", "let", "var"]
			}
		],

整合成一个git仓库呗,后面来的直接取了用

有人尝试过吗?我执行 npm i 就失败了。

➜ client git:(redwar3d) ✗ npm i
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://github.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See There’s Math.random(), and then there’s Math.random() · V8 for details.
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://github.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/

billiards_client@3.3.2 prepare
husky install

.git can’t be found (see https://git.io/Jc3F9)
npm ERR! code 1
npm ERR! path /Volumes/mac/myspace/red-war-client/client
npm ERR! command failed
npm ERR! command sh -c husky install

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/mac/.npm/_logs/2022-08-30T03_10_03_035Z-debug-0.log
➜ client git:(redwar3d) ✗ cd /Users/mac

包没问题那就是环境问题

有没有规矩 能判断代码中是否存在中文呢?

可以删了package.json 里面的husky ,然后手动 npm i --dev-save husky 试试

@typescript-eslint/naming-convention 可以使用正则表达式,当然这只是命名规则判读

“scripts”: {
“eslint”: “eslint ./assets/ --ext .ts --fix”,
//“prepare”: “husky install”,
“prettier”: “prettier --config .prettierrc.js --write ./assets/**/*.ts”
},
删除 “prepare”: “husky install”,
执行 npm i --dev-save husky 后 没有抛出错误。

Error: command ‘eslint.applySingleFix’ already exists
这是什么错误?

你npm包装完了没?vscode 插件装了没

好像重启了一下vscode 就好了