我已经设置了 eslint 来警告我 unsed vars
rules: {
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
}
现在我有一个看起来像这样的打字稿类:
import { User } from './user';
export class MyClass {
async myMethod(): Promise<User> {
// Some code
}
}
运行 eslint 时出现此错误
error 'User' is defined but never used @typescript-eslint/no-unused-vars
为什么 eslint 会警告我?在我看来,变量被使用了。
答案 0 :(得分:1)
在您的示例中,我们只看到 Promise 类型,这并不意味着您在方法中使用 User,而只是定义了一个类型。 预计 Eslint :
import { IUser} from './user'; // interface for User object
import { User } from './user'; // User object
export class MyClass {
async myMethod(User: string): Promise<IUser> {
// Some code with User parameter
}
}
您的 Eslint 规则仅适用于用户对象,而不适用于界面。
一些读数:https://basarat.gitbook.io/typescript/future-javascript/promise
答案 1 :(得分:0)
您必须禁用基本规则,因为它可能会报告不正确的错误。参考:@typescript-eslint/no-unused-vars
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { args: "none" }]
用户.ts
export class User {
name: string;
}
MyClass.ts
import { User } from './User'
export class MyClass {
async myMethod(): Promise<User> {
// Some code
}
}
这也是我的最小 eslint 配置
module.exports = {
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint/eslint-plugin"],
"env": {
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ['error'],
}
}
package.json 依赖
{
"scripts": {
"lint": "eslint hello.ts"
},
"devDependencies": {
"@types/eslint": "7.2.4",
"@types/node": "13.7.7",
"@typescript-eslint/eslint-plugin": "4.14.1",
"@typescript-eslint/parser": "4.14.1",
"eslint": "7.18.0",
"ts-node": "8.6.2",
"typescript": "3.8.3"
}
}