简体   繁体   English

类型“{}”中缺少属性“x”,但类型“Pick”中需要<Interface, "x"> &#39;。 TS2741

[英]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.

相关问题 TS2741 类型 &#39;{ 标签:字符串; 中缺少属性 &#39;0&#39; Javascript - TS2741 Property '0' is missing in type '{ label: string; } Javascript 类型“{}”中缺少属性“profileStore”,但类型“只读”中需要<AppProps> &#39;.ts(2741) - Property 'profileStore' is missing in type '{}' but required in type 'Readonly<AppProps>'.ts(2741) “DefaultRootState”类型中缺少属性“X”-但该属性是在接口中声明的 - Property 'X' is missing in type 'DefaultRootState' - but the property is declared in Interface 类型中缺少属性,但类型中需要属性 - Property is missing in type but required in type 类型 &#39;{ [x: string]: string; 中缺少属性 }&#39; - Property is missing in type '{ [x: string]: string; }' JS 文件中的 TS 警告:“X 类型上不存在 X 属性”:是否可以编写更干净的 JavaScript? - TS warning in JS file: "Property X does not exist on type X": is it possible to write cleaner JavaScript? 类型“{}”中缺少属性“submitAction”,但在类型中是必需的 - Property 'submitAction' is missing in type '{}' but required in type 所需类型“Type!”的变量“x” 没有提供 - Variable “x” of required type “Type!” was not provided Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' Rxjs v6.x 上的类型“事件”.ts(2339) 上不存在属性“clientX” - Property 'clientX' does not exist on type 'Event'.ts(2339) on Rxjs v6.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM