Skip to content

🐛 Bug: Wrong CartItems type #5

Closed

Description

@monarcode

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Getting wrong type / autocomplete suggestions for CartItems

// my type def
interface ViewListing { id: string; user_id: number; listing_category_id: string; listing_sub_category_id?: any; service_offering: string; description: string; listing_images: string[]; minimum_offer: string; slug: string; status: string; size: string; vehicle_id: number; country_id: string; listing_type: string; created_at: string; updated_at: string; }

Actual

// default type def
interface CartItems {
cartQuantity?: number;
imagesrc?: string;
inStock?: boolean;
name: string;
price?: number;
productId: number | string | undefined;
quantity: number;
}

Additional Info

Running on:

Nextjs app router
Cart v1.1.1

How can i overwrite or extend the default types for CartItems? the default defaults do not match the items i am saving to cart

Activity

  1. mcnaveen commented on Oct 22, 2023

    @mcnaveen
    Collaborator

    Hi @monarcode Can you try this?

    You can extend the CartItems interface.

    interface CustomCartItems extends CartItems {
      id: string;
      user_id: number;
      listing_category_id: string;
      listing_sub_category_id?: any;
      service_offering: string;
      description: string;
      listing_images: string[];
      minimum_offer: string;
      slug: string;
      status: string;
      size: string;
      vehicle_id: number;
      country_id: string;
      listing_type: string;
      created_at: string;
      updated_at: string;
    }

    Then just use CustomCartItems as your Type.

    Please let me know how it goes. Happy to help.

  2. monarcode commented on Oct 22, 2023

    @monarcode
    Author

    Hi @monarcode Can you try this?

    You can extend the CartItems interface.

    interface CustomCartItems extends CartItems {
      id: string;
      user_id: number;
      listing_category_id: string;
      listing_sub_category_id?: any;
      service_offering: string;
      description: string;
      listing_images: string[];
      minimum_offer: string;
      slug: string;
      status: string;
      size: string;
      vehicle_id: number;
      country_id: string;
      listing_type: string;
      created_at: string;
      updated_at: string;
    }

    Then just use CustomCartItems as your Type.

    Please let me know how it goes. Happy to help.

    Thank you for your response, i was able to get proper type hints by doing something similar

    interface Custom extends CartItems {
      ...
    }
    
    const cart = withSSR(useCart, (state) => state);
    const Items = cart?.cartItems as Custom[];
    

    but i'm not sure if this is the best approach, i was thinking of extending the type of CartItems via a d.ts file, but not sure how i can achieve that..any recommendations?

  3. mcnaveen commented on Oct 22, 2023

    @mcnaveen
    Collaborator

    Ah. You can do that.

    // custom.d.ts
    
    interface CustomCartItems extends CartItems {
      id: string;
      user_id: number;
      listing_category_id: string;
      listing_sub_category_id?: any;
      service_offering: string;
      description: string;
      listing_images: string[];
      minimum_offer: string;
      slug: string;
      status: string;
      size: string;
      vehicle_id: number;
      country_id: string;
      listing_type: string;
      created_at: string;
      updated_at: string;
    }

    Then in the main file.

    import { useCart, withSSR } from "cart";
    import { CustomCartItems } from './custom';
    
    const cart = withSSR(useCart, (state) => state);
    const items = cart?.cartItems as CustomCartItems[];
  4. mcnaveen commented on Oct 24, 2023

    @mcnaveen
    Collaborator

    Hi, @monarcode Since this is not an issue, I'll be moving this to Discussion. Feel free to follow up if needed.

    Thanks for understanding 😄

  5. locked and limited conversation to collaborators on Oct 24, 2023
  6. converted this issue into a discussion #6 on Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions