本文介绍了Standardjs 在使用时没有未使用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 Typescript React 项目中,我定义了:
导出类型 NavState = {mounted: boolean }
然后在我的组件中我使用了如下主题:
import { NavState } from '../../models/nav'类 Nav 扩展了 React.Component{状态:NavState = {安装:假}}
但我在导入中为 NavState
设置了红色下划线,它说:
'NavState' 已定义但从未使用.(no-unused-vars) 标准 (no-unused-vars)
在我的 package.json
中有这个:
"标准": {忽略": ["node_modules/**",**/__生成__/"],"parser": "@typescript-eslint/parser",插件":[@typescript-eslint"]}
而我的 vs-code settings.json
是这样的:
{standard.autoFixOnSave":真,标准.启用":真,"standard.run": "onType",标准.验证":[{ "language": "javascript", "autoFix": true },{ "language": "javascriptreact", "autoFix": true },{ "language": "typescript", "autoFix": true },{ "language": "typescriptreact", "autoFix": true }]}
为什么 Standardjs 无法理解我使用了类型别名?我该如何解决?
解决方案
这应该可以解决问题:
import { NavState } from '../../models/nav'//eslint-disable-line
注意导入行上的注释.
查看文档了解更多信息.>
In my Typescript React project, I defined:
export type NavState = { mounted: boolean }
and then in my component I used theme like:
import { NavState } from '../../models/nav'
class Nav extends React.Component<any, NavState> {
state: NavState = {
mounted: false
}
}
but I got red underline for NavState
in my import and it says:
'NavState' is defined but never used. (no-unused-vars)standard(no-unused-vars)
In my package.json
I have this:
"standard": {
"ignore": [
"node_modules/**",
"**/__generated__/"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
]
}
and my vs-code settings.json
is like this:
{
"standard.autoFixOnSave": true,
"standard.enable": true,
"standard.run": "onType",
"standard.validate": [
{ "language": "javascript", "autoFix": true },
{ "language": "javascriptreact", "autoFix": true },
{ "language": "typescript", "autoFix": true },
{ "language": "typescriptreact", "autoFix": true }
]
}
Why Standardjs can't understand that I used a type alias? and how can I fix it?
解决方案
This should do the trick:
import { NavState } from '../../models/nav' //eslint-disable-line
Notice the comment on the import line.
Check the docs for more.
这篇关于Standardjs 在使用时没有未使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!