Skip to content

Commit

Permalink
[Part of #176153] @kbn/es-types (#179758)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Apr 4, 2024
1 parent 88f1b5b commit 408b8fc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
51 changes: 51 additions & 0 deletions packages/kbn-es-types/src/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { AggregateOfMap } from './search';

xdescribe('AggregateOfMap', () => {
test('aggregations should assume buckets are there if type is explicit', () => {
type MyAggregation = {} & {
group_by: {
terms: unknown;
};
};

const aggregation = {} as unknown as AggregateOfMap<MyAggregation, unknown>;
aggregation.group_by.buckets.length.toFixed(); // using a number-specific method
});

test('aggregations should not assume buckets are there if the aggregation may be undefined', () => {
type MyAggregation =
| undefined
| ({} & {
group_by: {
terms: unknown;
};
});

const aggregation = {} as unknown as AggregateOfMap<MyAggregation, unknown>;
aggregation?.group_by.buckets.length.toFixed(); // using a number-specific method
// @ts-expect-error "aggregation" may be undefined
aggregation.group_by.buckets.length.toFixed(); // using a number-specific method
});

test('aggregations should not assume buckets are there if the bucket name may be undefined', () => {
type MyAggregation =
| {} & {
group_by?: {
terms: unknown;
};
};

const aggregation = {} as unknown as AggregateOfMap<MyAggregation, unknown>;
aggregation.group_by?.buckets.length.toFixed(); // using a number-specific method
// @ts-expect-error "group_by" may be undefined
aggregation.group_by.buckets.length.toFixed(); // using a number-specific method
});
});
3 changes: 1 addition & 2 deletions packages/kbn-es-types/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ export type AggregateOf<

export type AggregateOfMap<TAggregationMap extends AggregationMap | undefined, TDocument> = {
[TAggregationName in keyof TAggregationMap]: Required<TAggregationMap>[TAggregationName] extends AggregationsAggregationContainer
? // @ts-expect-error not sure how to fix this, anything I've tried causes errors upstream - Dario
AggregateOf<TAggregationMap[TAggregationName], TDocument>
? AggregateOf<Required<TAggregationMap>[TAggregationName], TDocument>
: never; // using never means we effectively ignore optional keys, using {} creates a union type of { ... } | {}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ export async function fetchSeries<T extends ValueAggregationMap>({
}

return response.aggregations.groups.buckets.map((bucket) => {
const bucketValue = bucket.value as ValueAggregationMap | undefined;
let value =
bucket.value?.value === undefined || bucket.value?.value === null
bucketValue?.value === undefined || bucketValue?.value === null
? null
: Number(bucket.value.value);
: Number(bucketValue.value);

if (value !== null) {
value =
Expand Down

0 comments on commit 408b8fc

Please sign in to comment.