![](/img/trans.png)
[英]TS2741 Property '0' is missing in type '{ label: string; } Javascript
[英]Property 'x' is missing in type '{}' but required in type 'Pick<Interface, "x">'. TS2741
我正在尝试将一些东西从 redux store 传递给带有connect
的组件。 这是我的代码:
家长:
export const MainPage = (
{
count,
handleIncrementClick,
selectedOfferId,
}: MainPageProps,
): React.ReactElement => {
const handleClick = (): Function => handleIncrementClick(count + 1);
return (
<div>
<h1 data-testid="FirstH1">{`Selected offer: ${selectedOfferId}`}</h1>
<button onClick={handleClick} type="button">
{`Klikłeś ${count} razy`}
</button>
<OffersContainer /> {/* It throws the error here */}
</div>
);
};
孩子:
容器:
const mapStateToProps = (state: globalStateType) => ({ dataset: state.offersDataSet, }); const mapDispatchToProps = (dispatch: Dispatch): FSetSelectedOfferIdInterface => ({ FSetSelectedOfferId: ( selectedOfferId: selectedOfferIdType, ) => dispatch(setSelectedOfferId(selectedOfferId)), }); export const OffersContainer = connect(mapStateToProps, mapDispatchToProps)(OffersComponent);
成分:
export const OffersComponent = ({ offersDataSet }: OffersPropsInterface): ReactElement => ( <> {offersDataSet.map(({ id, firstName, city, price, image, description, }): React.ReactElement => ( <FlexWrapper key={id.$oid}> <OfferContainer id={id} firstName={firstName} city={city} price={price} image={image} description={description} /> </FlexWrapper> ))} </> );
优惠道具界面:
export interface OffersPropsInterface {
offersDataSet: OfferPropsInterface[];
}
优惠道具界面:
export interface OfferPropsInterface {
id: {'$oid': string};
firstName: string;
city: string;
price: number;
image: string;
description: string;
}
这似乎是一个简单的问题,但由于某种原因,我收到此错误:
类型“{}”中缺少属性“offersDataSet”,但类型“Pick<OffersPropsInterface, "offersDataSet">”需要。 TS2741
为什么? 我没有渲染组件,而是渲染容器,它只需要offersDataSet
作为数组,它是通过 redux 提供的。 我怎样才能使offersDataSet
是强制性的,但仅适用于 redux,而不适用于渲染? 还是问题完全不同?
好吧,我很傻。 我将 redux 数据分配给了错误的名称。 这里:
const mapStateToProps = (state: globalStateType) => ({
dataset /* This should've been offersDataSet.*/: state.offersDataSet,
//^^^ offersDataSet
});
现在我知道为什么我需要所有类型的东西......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.