Skip to content

Amount for product variant is incorrectly typed, expected number, got string. #980

Open
@ViktorShev

Description

@ViktorShev
  • Is this a "how to" question? For questions on how to use the SDK, implement features or other related questions, please use our forums.
  • Is this something you can debug and fix? Create a pull request and contribute! Bug fixes and documentation fixes are welcome. You can read our contribution guidelines for more details.

Bug details

Describe the bug

ShopifyBuy.MoneyV2 interface declares amount as number, but in reality, a string is found on that field.

interface MoneyV2 {
  amount: number;
  currencyCode: CurrencyCode;
}

To Reproduce

You can fetch a product using client.product.fetch, then console.log(typeof product?.variants[0]?.price.amount)

image

Please include code samples:

The function I'm using to fetch a product:

export async function getProduct (filters: XOR<{ id: string }, { handle: string }>): AsyncResult<Product, ShopifyClientError> {
  try {
    if (!isNil(filters.id)) {
      const product = await shopifyClient.product.fetch(filters.id)

      if (isNil(product)) {
        return err(new ShopifyClientError('unexpected', `Product with ID ${filters.id} was null / undefined`))
      }

      return ok(product)
    }

    if (!isNil(filters.handle)) {
      const product = await shopifyClient.product.fetchByHandle(filters.handle)

      if (isNil(product)) {
        return err(new ShopifyClientError('unexpected', `Product with handle ${filters.handle} was null / undefined`))
      }

      return ok(product)
    }

    return err(new ShopifyClientError('invalid-input', 'A product ID or handle is required to fetch a product'))
  } catch (error: any) {
    return err(new ShopifyClientError('unknown', 'Failed to fetch single product', error))
  }
}

How I'm using the value:

<p className='mb-md text-2xl font-semibold font-rubik'>{formatMonetaryAmount(product?.variants[0]?.price.amount ?? 0, { currencyCode: product?.variants[0]?.price.currencyCode })}</p>

formatMonetaryAmount:

export function formatMonetaryAmount (amount: number, opts?: { currencyCode?: string, locale?: string }): string {
  if (typeof amount !== 'number') throw new Error('Amount must be a number!')

  return new Intl.NumberFormat(opts?.locale, {
    style: 'currency',
    currency: opts?.currencyCode ?? 'USD'
  }).format(amount)
}

Expected behavior

I expect the amount field to be of type number

Environment

  • OS: Debian 12
  • Browser: Google Chrome Version 120.0.6099.129 (Official Build) (64-bit)
  • SDK Version ^2.21.1
  • Types version: ^2.17.4

Bug Report Checklist

  • I have read and agree to the CODE_OF_CONDUCT.md
  • I have read the CONTRIBUTING.md guidelines.
  • I have provided a detailed description of the bug, including code samples, and/or network data.
  • I have provided information about my development environment, including SDK version.

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