前景提要
之前 webpack 配置的 jsx.后面上了 ts,再到后来开启了 eslint. 没有开启 eslint 的时候都能正常不报错。后面开启后我始终不知道这个应该怎么定义了! 代码: import * as React from 'react'; import {BaseProps} from '../commonProps'; import {Config, ClassNames, PropTypes} from '../component'; interface IIconProps extends BaseProps { name: string, color?: string, size?: number | string, onClick?: (e: React.MouseEvent<HTMLElement>) => void } const Icon: React.FC<IIconProps> = (props: IIconProps) => { const prefixCls = `${Config.prefix}-icon`; const {className, style, name, color, size, onClick} = props; const iconStyles = (): object => { const sty: React.CSSProperties = {fontSize: null, color: null}; if (Number.isInteger(size as number)) { sty.fontSize = `${size}px`; } else { sty.fontSize = size; } if (color) { sty.color = color; } return Object.assign(sty, {...style}); }; const cls = ClassNames( `${prefixCls}__${name}`, className ); const sty = iconStyles(); const handleClick = (e: React.MouseEvent<HTMLElement>) => { if (typeof onClick === 'function') { onClick(e); } }; return <i className={cls} style={sty} onClick={handleClick}/>; }; Icon.propTypes = { className: PropTypes.string, style: PropTypes.objectOf(PropTypes.object), name: PropTypes.string, color: PropTypes.string, size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), onClick: PropTypes.func }; export default Icon;
然后 eslint 报 Failed to compile. [at-loader] ./src/components/icon/Icon.tsx:39:6 TS2339: Property 'propTypes' does not exist on type '(props: any) => DetailedReactHTMLElement<{ className: any; style: any; onClick: (e: any) => void; }, HTMLElement>'.
我怎么定义它都是报这个错特来向大家请教,谢谢!