Eslint returns 没有未使用的变量
Eslint returns with no-unused-vars
我设置了 eslint 以警告我未使用 vars
rules: {
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
}
现在我有一个打字稿 class 看起来像这样:
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 警告我?在我看来,变量被使用了。
在你的例子中,我们只看到 Promise 类型,这并不意味着你在 Method 中使用了 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
您必须禁用基本规则,因为它会报告不正确的错误。参考:@typescript-eslint/no-unused-vars
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { args: "none" }]
User.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"
}
}
我设置了 eslint 以警告我未使用 vars
rules: {
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
}
现在我有一个打字稿 class 看起来像这样:
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 警告我?在我看来,变量被使用了。
在你的例子中,我们只看到 Promise 类型,这并不意味着你在 Method 中使用了 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
您必须禁用基本规则,因为它会报告不正确的错误。参考:@typescript-eslint/no-unused-vars
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { args: "none" }]
User.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"
}
}