'What type to use for terms aggregation?

I have an Elasticsearch terms aggregation to grab the unique values of a few fields:

    {
      // query removed for brevity
      aggs: {
        provinces: {
          terms: { field: "shipping_address.province.keyword" }
        },
        shipping_carriers: {
          terms: { field: "fulfillments.tracking_company.keyword" }
        },
        shipping_methods: {
          terms: { field: "shipping_lines.title.keyword" }
        }
      }
    }

This results in a response where the aggregations look like:

{
  "aggregations" : {
    "shipping_carriers" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "FedEx",
          "doc_count" : 31
        },
        //...removed for brevity
      ]
    },
    "provinces" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 8,
      "buckets" : [
         //...removed for brevity
      ]
    },
    "shipping_methods" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
         //...removed for brevity
      ]
    },
    "shipping_codes" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
         //...removed for brevity
      ]
    }
  }
}

I'm using the Javascript library with Typescript.

I'm still sort of new to Typescript, and I can't figure out what types to use for the buckets so I can access the key as a string.

      // ???: what are the correct types to use here?
      const aggs = results.aggregations as Record<
        string,
        AggregationsTermsAggregateBase<AggregationsCompositeBucketKeys>
      >;
      
      // ???: `any[]` is bad, what are the proper types to use?
      const province_buckets = aggs.provinces.buckets as any[];
      const carrier_buckets = aggs.shipping_carriers.buckets as any[];
      const method_buckets = aggs.shipping_methods.buckets as any[];

      return new OrderFilters({
        provinces: province_buckets.map((bucket) => bucket.key),
        shipping_carriers: carrier_buckets.map((bucket) => bucket.key),
        shipping_methods: method_buckets.map((bucket) => bucket.key)
      });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source