[英]Why do I get "Binding element 'store' implicitly has an 'any' type" here with TypeScript?
I have an array which contains 2 objects, I'd like to use map
to pass each object to a Card.我有一个包含 2 个对象的数组,我想使用
map
将每个对象传递给 Card。 Here is the code:这是代码:
The IStores
interface: IStores
接口:
interface IStores { id: number; title: string; } export default IStores;
The Card
component: Card
组件:
const Card = ({ store }) => { return ( <> <div className="card" key={store.id}> <div className='card-body'> <h5 className="card-title"> {store.title} </h5> </div> </div> </> ) } export default Card;
The Home
component which passes each object to Card
with map
:使用
map
将每个对象传递给Card
的Home
组件:
const storeDetails: IStores[] = [ { id: 1, title: 'Store A' }, { id: 2, title: 'Store B' } ] const Home = () => { return ( <> <div className='center-content'> <h1>Just shop local</h1> <div> { storeDetails.map( (d) => { return ( <Card store={d} /> ) } ) } </div> </div> </> ) } export default Home;
Now I've got an error in Card
at line 1:现在我在
Card
的第 1 行遇到了一个错误:
Binding element 'store' implicitly has an 'any' type
How can I fix it?我该如何解决?
Binding element 'store' implicitly has an 'any' type
绑定元素“store”隐式具有“any”类型
This error is telling you that you have a variable without a type.这个错误告诉你你有一个没有类型的变量。 So you have to type the props that
Card
expects.所以你必须输入
Card
期望的道具。 In this case, you might do that like:在这种情况下,您可以这样做:
interface Props {
store: IStores
}
const Card = ({ store }: Props) => {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.