简体   繁体   English

如何修复 TypeScript 接口错误?

[英]How to fix error with TypeScript interface?

I have a TypeScript question.我有一个 TypeScript 问题。

I set in state that either a certain interface or an empty object will get there.我在 state 中设置了某个接口或空的 object 将到达那里。 I take information for the component from this state. But this component swears that there cannot be an empty object in the state because of the interface.我从这个state获取组件的信息。但是这个组件发誓state中不能有一个空的state因为接口。 Tell me, please, how to configure so that TypeScript does not swear and the error disappears.请告诉我,如何配置才能使 TypeScript 不发誓并且错误消失。 Thanks!谢谢!

const OneNews: FC<NewsObjectInterface> = ({image, title, createdAt, preview, description}) => {


    return (
        <>
            <Header/>
            <div className="single-blog one-news">
                <div className="single-blog-img">
                    <img src={image} alt={`Image for ${title}`}/>
                </div>
                <div className="blog-meta">
                 <span className="date-type">
                      <i className="bi bi-calendar"></i>{createdAt}
                 </span>
                </div>
                <div className="blog-text">
                    <h4>{title}</h4>
                    <p>{preview}</p>
                    <p>{description}</p>
                </div>
            </div>
            <Footer/>
        </>
    )
}

export default OneNews;



export interface NewsObjectInterface {
    createdAt: string,
    description: string,
    id: string,
    image: string,
    preview: string,
    title: string,
}






const [oneNewsData, setOneNewsData] = useState<NewsObjectInterface | {}>({});

<Route path="/one-news"
                   element={<OneNews title={oneNewsData.title} preview={oneNewsData.preview} image={oneNewsData.image} id={oneNewsData.id} createdAt={oneNewsData.createdAt} description={oneNewsData.description}/>}/>
        </Routes>

You have declared your state to be of type:您已将 state 声明为以下类型:

NewsObjectInterface | {}

Which means you either have an object full of your expected properties, or you have an empty object. And when you do oneNewsData.title Typescript enforces that both sides of that union must have that property.这意味着您要么拥有一个充满预期属性的 object,要么拥有一个空的 object。当您执行oneNewsData.title Typescript 时,强制该联合的双方都必须具有该属性。 And an empty object has no properties.空的 object 没有任何属性。


It seems like you need a temporary value while the real value loads.似乎在加载实际值时需要一个临时值。 In this case I would recommend you change the type of your state to:在这种情况下,我建议您将 state 的类型更改为:

useState<NewsObjectInterface | null>(null);

Now the default value is null .现在默认值为null Something else may set a NewsObjectInterface in state later.稍后可能会在 state 中设置一个NewsObjectInterface

And then you simply check for null before you use the state value:然后您只需在使用 state 值之前检查null

<Route path="/one-news"
  element={
    oneNewsData && <OneNews // check oneNewsData before use
      title={oneNewsData.title}
      preview={oneNewsData.preview}
      image={oneNewsData.image}
      id={oneNewsData.id}
      createdAt={oneNewsData.createdAt}
      description={oneNewsData.description}
    />
  }
/>

Change your interface like this (mind the ? symbol):像这样更改您的界面(注意?符号):

export interface NewsObjectInterface {
    createdAt?: string,
    description?: string,
    id?: string,
    image?: string,
    preview?: string,
    title?: string,
}

This should be enough.这应该足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM