在一个TypeScript React项目中,我正在使用
react-dnd及其
DefinitelyTyped typings:
interface ExampleScreenProps { a,b,c } interface ExampleScreenState { x,y,z } class ExampleScreen extends React.Component<ExampleScreenProps,ExampleScreenState> { } export default DragDropContext(HTML5Backend)(ExampleScreen);
这被渲染在另一个组件中:
import ExampleScreen from "./ExampleScreen"; <ExampleScreen a="a" b="b" c="c" />
这在TS 1.8没有任何错误.当我升级到TS 2.0时,我得到以下编译错误:
Error:(90,10) TS2600: JSX element attributes type
‘(ExampleScreenProps & { children?: ReactNode; }) |
(ExampleScreenProps & { children…’ may not be a union type.
这是type definition for DragDropContext
:
export function DragDropContext<P>( backend: Backend ): <P>(componentClass: React.ComponentClass<P> | React.StatelessComponent<P>) => ContextComponentClass<P>;
我不能把它放在一起抱怨的错误是什么?似乎它不喜欢ComponentClass< P> | StatelessComponent< P>,但是那些不是元素属性,元素属性简单地是< P>.我试图明确地通过< P> ;::
export default DragDropContext<ExampleProps>(HTML5Backend)(ExampleScreen);
但同样的错误仍然存在.我可以通过断言输出来解决它:
export default DragDropContext(HTML5Backend)(ExampleScreen) as React.ComponentClass<ExampleProps>;
但我不喜欢使用断言,我不明白实际的问题,或者我做错了事情.这是可以解决的打字问题吗?
您可以使用以下方式安装新的打字:
npm i @types/react-dnd --save-dev
如果您已经安装其他打字,请使用:
typings uninstall --save --global react-dnd
之后,它应该按预期工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。