Javascript 键入脚本、作出反应并重新组合问题
为这个模糊的标题提前道歉这很难理解;博士,我的问题 我试图同时使用React、Recompose和Typescript,但在尝试将道具传递到我的组件时,出现以下错误:Javascript 键入脚本、作出反应并重新组合问题,javascript,reactjs,typescript,recompose,Javascript,Reactjs,Typescript,Recompose,为这个模糊的标题提前道歉这很难理解;博士,我的问题 我试图同时使用React、Recompose和Typescript,但在尝试将道具传递到我的组件时,出现以下错误: [ts]属性“bar”在类型“IntrinsicatAttributes&IntrinsicClassAttributes&Readonly&Readonly”上不存在。 组件的导入方式如下所示: 从“/TestComponent”导入TestComponent 并这样称呼: 以下是“我的组件”文件夹的结构: ├── TestC
[ts]属性“bar”在类型“IntrinsicatAttributes&IntrinsicClassAttributes&Readonly&Readonly”上不存在。
组件的导入方式如下所示:
从“/TestComponent”导入TestComponent
并这样称呼:
以下是“我的组件”文件夹的结构:
├── TestComponent.tsx
├── TestComponentContainer.ts
└── index.ts
index.ts
如下:
import TestComponentContainer from './TestComponentContainer'
export default TestComponentContainer
import * as React from 'react'
interface IProps {
foo: string
bar: string
}
const TestComponent: React.SFC<IProps> = ({ foo, bar }) => (
<div>
<p>{foo}</p>
<p>{bar}</p>
</div>
)
export default TestComponent
import { compose, withProps } from 'recompose'
import TestComponent from './TestComponent'
export default compose(
withProps({
foo: 'stringy string string',
}),
)(TestComponent)
TestComponent.jsx
如下所示:
import TestComponentContainer from './TestComponentContainer'
export default TestComponentContainer
import * as React from 'react'
interface IProps {
foo: string
bar: string
}
const TestComponent: React.SFC<IProps> = ({ foo, bar }) => (
<div>
<p>{foo}</p>
<p>{bar}</p>
</div>
)
export default TestComponent
import { compose, withProps } from 'recompose'
import TestComponent from './TestComponent'
export default compose(
withProps({
foo: 'stringy string string',
}),
)(TestComponent)
我知道我错过了一些道具的类型声明,这些道具正在被传递,但我一辈子都不能确切地知道我应该做什么
编辑:
此外,如何使用嵌套方法实现这一点:
export default compose(
setDisplayName('PlayerCardContainer'),
withMutation(mutation),
withHandlers(createHandlers),
)(PlayerCard)
和允许您指定结果组件的类型以及可以调用该组件的组件的类型
因此,最好避免编写HOC调用,而只嵌套HOC调用:
import { withProps } from 'recompose';
import TestComponent from './TestComponent';
export default withProps({
foo: 'stringy string string',
})(TestComponent);
编辑:
对于添加的代码示例,嵌套HOC而不是使用compose
如下所示:
export default setDisplayName('PlayerCardContainer')(
withMutation(mutation)(
withHandlers(createHandlers)(
PlayerCard
)
)
);
不错的字符串值,顺便说一句,不要担心,重要的是要记住
setDisplayName('PlayerCardContainer')
、withMutation(mutation)
和withHandlers(createHandlers)
所有返回函数compose
只是在调用下一个的结果时调用每一个的语法糖。它将嵌套函数调用转换为一个漂亮的平面列表,但仅此而已。如果打字改进了,那么保留语法糖分就更好了,但是在打字改进之前,嵌套函数会更安全,这样TypeScript就可以准确地知道发生了什么,并且可以有效地执行静态打字。啊哈,我刚刚正确地点击了它,非常感谢!呵呵。这个我可能会打开一个pull请求来更改它,没有理由setDisplayName
无法推断类型。同时,您可以将{bar:string}
作为类型参数传递,如下所示:setDisplayName(…
update:the),这样您就可以删除版本为0.27.0或更高版本的@types/recompose的setDisplayName
的类型参数