简体   繁体   English

为什么我在这里使用 TypeScript 得到“绑定元素‘商店’隐式具有‘任何’类型”?

[英]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将每个对象传递给CardHome组件:

 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) => {
  //...
}

Playground 操场

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

相关问题 映射打字稿参数:绑定元素“列”隐式具有“任何”类型 - Map typescript parameter: Binding element 'columns' implicitly has an 'any' type 为什么打字稿抛出绑定元素“目标”隐式具有“任何”type.ts(7031)? - why typescript throws Binding element 'target' implicitly has an 'any' type.ts(7031)? 绑定元素“x”隐式具有“任何”类型 - Binding element 'x' implicitly has an 'any' type TypeScript:元素隐含地具有 RegExp 的“任何”类型 - TypeScript: Element implicitly has an 'any' type for RegExp TypeScript 元素隐含一个“任何” - TypeScript Element implicitly has an 'any' React typescript 错误:元素隐式具有“任何”类型 - React typescript error: Element implicitly has an 'any' type 如何修复错误“元素隐式具有‘任何’类型,因为类型的表达式 - How do I fix the error "Element implicitly has an 'any' type because the expression of type TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type Typescript,联合类型,元素隐含“任何” - Typescript, Union types, Element implicitly has an 'any' RN打字稿中参数隐式具有任何类型 - Parameter implicitly has any type in RN typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM