Type error: ‘property does not exist on type’ when it in fact does exist.

huangapple go评论75阅读模式
英文:

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 &#39;imageUrl&#39; does not exist on type &#39;Product&#39;.
  43 |       &lt;div className=&quot;flex items-center&quot;&gt;
  44 |         &lt;Image
&gt; 45 |           src={product.imageUrl}
     |                        ^
  46 |           alt={product.itemName}
  47 |           width={100}
  48 |           height={100}

CartItem.tsx:

import { Product } from &quot;types/types&quot;;
import Image from &quot;next/image&quot;;

export default function CartItem({ product }: { product: Product }) {
/* ... */

  return (
      &lt;div className=&quot;flex items-center&quot;&gt;
        &lt;Image
          src={product.imageUrl}
          alt={product.itemName}
          width={100}
          height={100}
          className=&quot;h-10 w-10 rounded-full mr-4 object-cover&quot;
        /&gt;
       {* ... *}
      &lt;/div&gt;
  );
}

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
  );
}

TS Playground

英文:

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&lt;CartItemTwoProps&gt; = ({ product }) =&gt; {
    return (
      product.imageUrl // works
  );
}

TS Playground

huangapple
  • 本文由 发表于 2023年7月20日 18:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定