Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug: Wrong CartItems type #5

Closed
3 tasks done
monarcode opened this issue Oct 20, 2023 · 4 comments
Closed
3 tasks done

馃悰 Bug: Wrong CartItems type #5

monarcode opened this issue Oct 20, 2023 · 4 comments

Comments

@monarcode
Copy link

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

@mcnaveen
Copy link
Owner

mcnaveen commented Oct 22, 2023

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.

@monarcode
Copy link
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?

@mcnaveen
Copy link
Owner

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[];

@mcnaveen
Copy link
Owner

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 馃槃

Repository owner locked and limited conversation to collaborators Oct 24, 2023
@mcnaveen mcnaveen converted this issue into discussion #6 Oct 24, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants