google / schema-dts

JSON-LD TypeScript types for Schema.org vocabulary
Apache License 2.0
860 stars 32 forks source link

Wrong type picked on using WithContext #186

Open milindsingh opened 1 year ago

milindsingh commented 1 year ago

I defined a wrapper class to store data, like

export interface MyProduct {
  url: string;
  image?: string;
  name: string;
  sku?: string;
  keywords?: string;
  description?: string;
  schema?: WithContext<Product>;
  meta?: Meta;
}

But when I assign a Product object, it recognizes as Vehicle and throw type error.

const product: MyProduct = {
        "schema": {
            "offers": [
                {
                    "seller": {
                        "name": "XYZ",
                        "@type": "Organization"
                    },
                    "priceCurrency": "INR",
                    "availability": "http://schema.org/InStock",
                    "@type": "Offer",
                    "url": "https://www.xyz.com/en_in/productpage.0874876001.html",
                    "price": "1499.0"
                }
            ],
            "image": "//www.xyz.com/image.jpg",
            "color": "Pigeon blue/Floral",
            "@type": "Product",
            "name": "Fitted cotton sheet",
            "description": "Fitted sheet with an all-over print on fine-threaded, closely-woven cotton with a thread count of 144. Fits mattresses up to 23 cm deep.",
            "sku": "1234567",
            "category": {
                "name": "Sheets",
                "@type": "Thing"
            },
            "@context": "https://schema.org",
            "brand": {
                "name": "XYZ",
                "@type": "Brand"
            },
            "itemCondition": "https://schema.org/NewCondition"
        },
        "image": "//www.xyz.com/image.jpg",
        "meta": {
            "parser": "schema",
            "modifiedDate": "2022-09-14T14:09:18.639Z",
            "createdDate": "2022-09-14T14:09:18.639Z"
        },
        "name": "Fitted cotton sheet",
        "description": "Fitted sheet with an all-over print on fine-threaded, closely-woven cotton with a thread count of 144. Fits mattresses up to 23 cm deep.",
        "sku": "0874876001",
        "url": "https://www.xyz.com/en_in/productpage.0874876001.html"
      }

Error:

Type '"Product"' is not assignable to type '"MotorizedBicycle"'.ts(2322)

Any help or guidance would be appreciated. Thanks

Eyas commented 1 year ago

The error is misleading, because type matching is weird, but if you look at the editor you'll see a few errors at a few degrees of nesting:

Fixing that should fix this error!