为什么ESLint为TypeScript接口抛出“ no-unused-vars”? [英] Why ESLint throws 'no-unused-vars' for TypeScript interface?
问题描述
所以,我在.ts文件中有这段代码:
So, I have this piece of code in .ts file:
import {MicroEventInterface} from '../Interfaces';
export default class MicroEvent implements MicroEventInterface {
// code
ESLint会引发此错误:
And ESLint throws this error:
I在ESLint中具有TypeScript的配置:
I have this config for TypeScript in ESLint:
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'
}
}
}
所以,一切似乎都很好,但是我无法克服这个错误。
So, everything seems like fine, but I can't conquer this error.
有什么建议吗?
谢谢!
UPD:
看起来好像我 console.log(
--- ,MicroEventInterface);
错误消失了。我认为,ESLint不会将实现
视为实际用法。
Looks like if I console.log(
--- , MicroEventInterface);
error disappears. I think, ESLint does not treat implements
as actual usage.
推荐答案
我认为解决此问题的正确方法是声明您的接口。
I believe that the correct way of solving this issue is to declare your interface.
declare interface Foo {}
以及其他类型,例如Foo.d.ts
in a separate file along with other types for example Foo.d.ts
然后正确配置您的打字稿编译器以提取所有声明。
And after that correctly configure your typescript compiler to pickup all of the declarations.
请参阅: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
部分: @ types,typeRoots和类型
之后,您将可以使用所有类型声明和没有显式导入的接口。
After that you will be able to use all of your type declarations and interfaces without explicit import.
这篇关于为什么ESLint为TypeScript接口抛出“ no-unused-vars”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!