shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

Provide more complete typings by decoupling object field descriptions and functionality

Open tkalliom opened this issue 2 years ago • 0 comments

Overview/summary

Decouple data structure typings from current REST resource classes into types or interfaces. Then the typings can be made a lot more useful with nested objects having proper typings, instead of being just Record<string, unknown>.

Motivation

What inspired this enhancement?

Currently, if you do something like for (const lineItem of order.line_items), lineItem is just { [key: string]: unknown }. This means missing out on the benefits of typings. It would be desirable for nested objects to be typed as well. (Contrast with https://github.com/MONEI/Shopify-api-node which has richer types like this.)

Here's an excerpt of what Order currently looks like:

export class Order extends Base {
  public static API_VERSION = ApiVersion.April22;
  // other static members

  public static async find(...): Promise<Order | null> {
    ...
  }
  // other methods

  public line_items: {[key: string]: unknown}[] | null;
  public app_id: number | null;
  public billing_address: {[key: string]: unknown} | null;
  // other fields
}

I'm thinking of something along the lines of:

interface IOrder {
  public line_items: {
    fulfillable_quantity: number;
    // other fields
  }[] | null;

  public app_id: number | null;

  public billing_address: ICustomerAddress | null;

  // other fields
}

export class Order extends Base, IOrder {
  // static members and methods
}

tkalliom avatar Apr 27 '22 13:04 tkalliom