英文:
Type error: 'property does not exist on type' when it in fact does exist
问题
以下是翻译好的部分:
错误信息:
类型错误:属性 'imageUrl' 在类型 'Product' 上不存在。
43 | <div className="flex items-center">
44 | <Image
> 45 | src={product.imageUrl}
| ^
46 | alt={product.itemName}
47 | width={100}
48 | height={100}
CartItem.tsx:
import { Product } from "types/types";
import Image from "next/image";
export default function CartItem({ product }: { product: Product }) {
/* ... */
return (
<div className="flex items-center">
<Image
src={product.imageUrl}
alt={product.itemName}
width={100}
height={100}
className="h-10 w-10 rounded-full mr-4 object-cover"
/>
{/* ... */}
</div>
);
}
types.tsx:
/* ... */
export type Product = {
id: number;
restaurantId: number;
itemName: string;
restaurantName: string;
imageUrl: string;
// title: string;
description: string;
price: number;
// discountPercentage: number;
// rating: number;
// stock: number;
// brand: string;
// category: string;
// thumbnail: string;
// images: string[];
quantity?: number;
};
英文:
I'm getting an error during the build process (Vercel) with product
that's passed into CartItem.tsx
even though it's declared in types.tsx
.
The error:
Type error: Property 'imageUrl' does not exist on type 'Product'.
43 | <div className="flex items-center">
44 | <Image
> 45 | src={product.imageUrl}
| ^
46 | alt={product.itemName}
47 | width={100}
48 | height={100}
CartItem.tsx:
import { Product } from "types/types";
import Image from "next/image";
export default function CartItem({ product }: { product: Product }) {
/* ... */
return (
<div className="flex items-center">
<Image
src={product.imageUrl}
alt={product.itemName}
width={100}
height={100}
className="h-10 w-10 rounded-full mr-4 object-cover"
/>
{* ... *}
</div>
);
}
types.tsx:
/* ... */
export type Product = {
id: number;
restaurantId: number;
itemName: string;
restaurantName: string;
imageUrl: string;
// title: string;
description: string;
price: number;
// discountPercentage: number;
// rating: number;
// stock: number;
// brand: string;
// category: string;
// thumbnail: string;
// images: string[];
quantity?: number;
};
答案1
得分: 1
Based on the code you've written there it should work. I would try moving your type definition to the same file as CartItem
temporarily to see if that resolves the problem - if it does then there must be a problem with how your type is imported.
Also a side note: you can use the type React.FC to more cleanly type components.
type CartItemTwoProps = { product: Product };
const CartItemTwo: React.FC<CartItemTwoProps> = ({ product }) => {
return (
product.imageUrl // works
);
}
英文:
Based on the code you've written there it should work. I would try moving your type definition to the same file as CartItem
temporarily to see if that resolves the problem - if it does then there must be a problem with how your type is imported.
Also a side note: you can use the type React.FC to more cleanly type components.
type CartItemTwoProps = { product: Product };
const CartItemTwo: React.FC<CartItemTwoProps> = ({ product }) => {
return (
product.imageUrl // works
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论