我正在尝试设置一个typescript-react-eslint
项目并且无法通过此样板组件的 eslint 错误:
import * as React from "react";
interface ButtonProps {
children?: React.ReactNode,
onClick?: (e: any) => void,
}
const styles = {
border: "1px solid #eee",
borderRadius: 3,
backgroundColor: "#FFFFFF",
cursor: "pointer",
fontSize: 15,
padding: "3px 10px",
margin: 10
};
const Button: React.FunctionComponent<ButtonProps> = props => (
<button onClick={props.onClick} style={styles} type="button">
{props.children}
</button>
);
Button.defaultProps = {
children: null,
onClick: () => {}
};
export default Button;
错误是:
19:26 error 'onClick' is missing in props validation react/prop-types
20:12 error 'children' is missing in props validation react/prop-types
似乎是在抱怨<button>
未定义html 的接口?否则它可能是Button
组件本身,但它不应该从<ButtonProps>
我传递的接口中获取类型信息吗?
我尝试明确设置children
并onClick
像这样:
Button.propTypes = {
children?: React.ReactNode,
onClick?: (e: any) => void
};
它绕过了 eslint 错误,但组件本身停止工作。我究竟做错了什么?
PS这是我的 .eslintrc.json
{
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/eslint-recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
}
}