当使用Realth.SFC时,Type Script给出以下错误:
Type '({ children }: { children?: ReactNode; }) => Element' is not
assignable to type 'StatelessComponent<{}>'.
Type 'Element' is not assignable to type 'ReactElement<any>'.
Types of property 'type' are incompatible.
Type 'string | ComponentClass<any> | StatelessComponent<any>' is not assignable to type 'string | ComponentClass<any, any> | StatelessComponent<any>'.
Type 'ComponentClass<any>' is not assignable to type 'string | ComponentClass<any, any> | StatelessComponent<any>'.
Type 'ComponentClass<any>' is not assignable to type 'StatelessComponent<any>'.
Types of property 'propTypes' are incompatible.
Type 'React.ValidationMap<any> | undefined' is not assignable to type 'import("/Users/jbernal/Documents/projects/BoostMobileUI/node_modules/@types/prop-types/index").ValidationMap<any> | undefined'.
Type 'React.ValidationMap<any>' is not assignable to type 'import("/Users/jbernal/Documents/projects/BoostMobileUI/node_modules/@types/prop-types/index").ValidationMap<any>'.
Index signatures are incompatible.
Type '((object: any, key: string, componentName: string, ...rest: any[]) => Error | null) | undefined' is not assignable to type 'Validator<any>'.
Type 'undefined' is not assignable to type 'Validator<any>'.
这是代码:
import * as React from 'react';
import './Sidebar.scss';
export const Sidebar: React.SFC = ({ children }) => (
<aside className="sidebar">
{children}
</aside>
);
我在其他地方也遇到了validationmap问题,例如,当将这种类型的组件作为工具提示的子级传入material的react ui库时。
最佳答案
我也有这个问题,来到这里寻找答案。
结果,我在一个纱线工作区monorepo上工作,有多个node_modules
使用不同版本的@types/react
。
以下是我解决问题的方法:
在每个package.json
使所有依赖项指向相同的版本;
删除所有node_modules
文件夹(也可以删除yarn.lock
文件);
运行yarn install
关于reactjs - React.SFC给出Element vs ReactElement <any>错误,ValidationMap,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51897871/