Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: product types from rest api #763

Open
Z3rio opened this issue Feb 16, 2024 · 2 comments
Open

question: product types from rest api #763

Z3rio opened this issue Feb 16, 2024 · 2 comments

Comments

@Z3rio
Copy link

Z3rio commented Feb 16, 2024

Hello, quick question. How are product types and such imported from the Rest API?

I've tried to import them from the RestResources object, as that's the only types being exported, but they do not seem to be useable, unless I'm missing something.

example:

import { RestResources } from "@shopify/shopify-api/rest/admin/2024-01";
import { shopify } from './utils/shopify' // setup of shopify API obj

function test(prod: RestResources["Product"] | null): void {
  console.log("hello world");
}

(async () => {
  // act like session & productId is defined
  const prod = await shopify.rest.Product.find({
    session: session,
    id: productId
  })
  
  test(prod);
})

This would result in the following error, as the types do not match:

- error TS2345: Argument of type 'Product' is not assignable to parameter of type 'typeof Product'.
  Type 'Product' is missing the following properties from type 'typeof Product': prototype, apiVersion, hasOne, hasMany, and 16 more.
@Z3rio
Copy link
Author

Z3rio commented Feb 16, 2024

Seems like we could technically use this:

type ShopifyProduct = NonNullable<Awaited<ReturnType<typeof shopify["rest"]["Product"]["find"]>>>

But that just seems dumb & overcomplicated in my opinion, surely there should be some exported types for this, no?

@paulomarg
Copy link
Contributor

paulomarg commented Feb 21, 2024

Hi, thanks for raising this. I think this is an interesting issue. A simpler way to get test() to work would be to declare it like this:

function test(prod: InstanceType<RestResources['Product']> | null): void {
  // ...
}

By using the InstanceType utility, you can define the prod parameter as being an instance of that class. While still a little more complicated than it has to be, this might unblock you.

I'll keep this open so we can evaluate exporting the classes as types, but there are other things that are going to be higher priority!

@paulomarg paulomarg transferred this issue from Shopify/shopify-api-js Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants