所以,我在 .ts 文件中有这段代码:
import {MicroEventInterface} from '../Interfaces';
export default class MicroEvent implements MicroEventInterface {
// code
Run Code Online (Sandbox Code Playgroud)
而 ESLint 会抛出这个错误:
我在 ESLint 中有这个 TypeScript 配置:
typescript: {
extends: [
'plugin:@private/private/react' // private rep with React config
],
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'import'
],
settings: {
'import/resolver': {
'node': {
'extensions': [
'.js',
'.jsx',
'.ts',
'.tsx'
],
'moduleDirectory': [
'node_modules/',
'src/'
]
}
},
react: {
createClass: 'createClass',
pragma: 'React',
version: '0.14.9'
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,一切似乎都很好,但我无法克服这个错误。
有什么建议?
谢谢!
更新:
看起来如果我console.log( …
Types我目前收到关于or内的函数参数的 eslint 警告Interfaces。
执行以下操作:
type Test = {
fn: (param: string) => void
}
Run Code Online (Sandbox Code Playgroud)
结果出现以下警告:
'param' is defined but never used. Allowed unused args must match /^_/u.
Run Code Online (Sandbox Code Playgroud)
这是我的.eslintrc.json样子:
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json"
},
"env": {
"node": true
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
Run Code Online (Sandbox Code Playgroud)
封装版本:
"eslint": "^7.23.0"
"@typescript-eslint/eslint-plugin": "^4.22.1"
"@typescript-eslint/parser": "^4.22.1"
Run Code Online (Sandbox Code Playgroud)
类似的问题已经在这里和这里被问过,但提供的答案似乎并不适合我。
对于可能发生的情况有什么建议吗?
我在具有以下设置的所有Typescript项目中使用esLint:
"extends": ["airbnb", "prettier", 'plugin:vue/recommended'],
"plugins": ["prettier"],
"parserOptions": {
"parser": "@typescript-eslint/parser",
"ecmaVersion": 2018,
"sourceType": "module"
},
Run Code Online (Sandbox Code Playgroud)
+一堆自定义规则。我还为Typescript支持安装了以下依赖项:
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
Run Code Online (Sandbox Code Playgroud)
但是,esLint最有用的规则之一https://eslint.org/docs/rules/no-unused-vars对于Typescript项目而言似乎配置很差。例如,当我导出一个枚举时,该规则警告我该枚举在声明它的文件中没有被使用:
export enum Foo {
Bar,
}
Run Code Online (Sandbox Code Playgroud)
类似地,当我导入要用作类型的接口或类时,“ no-unused-vars”将再次在实际导入的行中抱怨:
在脚
export interface Foo {
bar: string;
}
Run Code Online (Sandbox Code Playgroud)
在bar.ts中
import { Foo } from './Foo'
const bar: Foo = { bar: 'Hello' };
Run Code Online (Sandbox Code Playgroud)
有什么方法可以配置no-unused-vars规则来考虑这两种情况?我不喜欢禁用该规则,因为在这些案例之外,它是我整个规则集中最有用的规则之一。
我已经将规则降级为仅给出警告而不是错误,但是将我的所有文档都填充为警告仍然无法达到使用esLint的目的。