[英]Property 'x' is missing in type '{}' but required in type 'Pick<Interface, "x">'. TS2741
I am trying to pass some things from redux store to a component with connect
.我正在尝试将一些东西从 redux store 传递给带有connect
的组件。 This is my code:这是我的代码:
Parent:家长:
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>
);
};
Child:孩子:
Container:容器:
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);
Component:成分:
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> ))} </> );
OffersPropsInterface:优惠道具界面:
export interface OffersPropsInterface {
offersDataSet: OfferPropsInterface[];
}
OfferPropsInterface:优惠道具界面:
export interface OfferPropsInterface {
id: {'$oid': string};
firstName: string;
city: string;
price: number;
image: string;
description: string;
}
This seems like a simple problem, but for some reason I get this error:这似乎是一个简单的问题,但由于某种原因,我收到此错误:
Property 'offersDataSet' is missing in type '{}' but required in type 'Pick<OffersPropsInterface, "offersDataSet">'.类型“{}”中缺少属性“offersDataSet”,但类型“Pick<OffersPropsInterface, "offersDataSet">”需要。 TS2741 TS2741
Why though?为什么? I'm not rendering the component, instead I'm rendering the container, which requires only the offersDataSet
as an array, which is provided through redux.我没有渲染组件,而是渲染容器,它只需要offersDataSet
作为数组,它是通过 redux 提供的。 How can I make it so that the offersDataSet
is mandatory, but only for redux, not for rendering?我怎样才能使offersDataSet
是强制性的,但仅适用于 redux,而不适用于渲染? Or is the problem something entirely different?还是问题完全不同?
Ok I'm stupid.好吧,我很傻。 I was assigning the redux data to a wrong name.我将 redux 数据分配给了错误的名称。 Here:这里:
const mapStateToProps = (state: globalStateType) => ({
dataset /* This should've been offersDataSet.*/: state.offersDataSet,
//^^^ offersDataSet
});
Now I know why I need types for everything...现在我知道为什么我需要所有类型的东西......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.