使用 ESLint 禁止 MouseEvent (Typescript)
我试图强制执行一条规则来禁止这样的事情:
const submit = (event: React.MouseEvent) => {};
而是这样做:
const submit: React.MouseEventHandler = (event) => {};
我尝试过这样的事情:
"no-restricted-imports": ["error", {
"paths": [{
"name": "react",
"importNames": ["MouseEvent"],
"message": "Please use MouseEventHandler."
}]
}]
但只有当我这样做时它才有效:
import { MouseEvent } from 'react';
如果人们像这样使用它:React.MousEvent
ESLint 不会出错。有什么想法吗?
I am trying to enforce a rule to disallow things like this:
const submit = (event: React.MouseEvent) => {};
and instead do this:
const submit: React.MouseEventHandler = (event) => {};
I tried with something like this:
"no-restricted-imports": ["error", {
"paths": [{
"name": "react",
"importNames": ["MouseEvent"],
"message": "Please use MouseEventHandler."
}]
}]
But it only works if I do this:
import { MouseEvent } from 'react';
If people use it like this: React.MousEvent
ESLint doesn't error. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用名为 typescript-eslint/ban-types
您可以将其添加到您的
.eslintrc.js
文件中You could use ESLint rule called typescript-eslint/ban-types
You can add this in your
.eslintrc.js
file