From c3942dd0b4f9372f2928ac633d66da992b8cdfd4 Mon Sep 17 00:00:00 2001 From: dej611 Date: Tue, 12 Mar 2024 15:09:13 +0100 Subject: [PATCH 1/2] :label: Make type generic --- .../form_based/operations/definitions/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts index fd6b68c2bf65d..c7f8ed1e6923d 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts @@ -731,11 +731,13 @@ export type OperationType = string; * This is an operation definition of an unspecified column out of all possible * column types. */ -export type GenericOperationDefinition = - | OperationDefinition - | OperationDefinition - | OperationDefinition - | OperationDefinition; +export type GenericOperationDefinition< + ColumnType extends BaseIndexPatternColumn = BaseIndexPatternColumn +> = + | OperationDefinition + | OperationDefinition + | OperationDefinition + | OperationDefinition; /** * List of all available operation definitions From 0449751a3a563fd4af6c8fe309b3ad481beba312 Mon Sep 17 00:00:00 2001 From: dej611 Date: Tue, 12 Mar 2024 15:09:26 +0100 Subject: [PATCH 2/2] :white_check_mark: Fix type issue --- .../definitions/formula/formula.test.tsx | 15 +++++++++------ .../definitions/formula/mocks/operation_mocks.ts | 8 +++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx index 06a118dc165ce..26703a2eebbf3 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx @@ -17,7 +17,7 @@ import { insertOrReplaceFormulaColumn } from './parse'; import type { FormBasedLayer } from '../../../types'; import { IndexPattern } from '../../../../../types'; import { TermsIndexPatternColumn } from '../terms'; -import { MovingAverageIndexPatternColumn } from '../calculations'; +import type { MovingAverageIndexPatternColumn } from '../calculations'; import { StaticValueIndexPatternColumn } from '../static_value'; import { getFilter } from '../helpers'; import { createOperationDefinitionMock } from './mocks/operation_mocks'; @@ -43,6 +43,7 @@ const operationDefinitionMap: Record = { terms: createOperationDefinitionMock('terms', {}, { scale: 'ordinal' }), sum: createOperationDefinitionMock('sum', { filterable: true }), last_value: createOperationDefinitionMock('last_value', { + input: 'field', getPossibleOperationForField: jest.fn(({ type }) => ({ scale: type === 'string' ? 'ordinal' : 'ratio', isBucketed: false, @@ -54,14 +55,14 @@ const operationDefinitionMap: Record = { filterable: true, canReduceTimeRange: true, }), - derivative: createOperationDefinitionMock('derivative', { input: 'fullReference' }), - moving_average: createOperationDefinitionMock('moving_average', { - // @ts-expect-error upgrade typescript v4.9.5 + derivative: createOperationDefinitionMock('derivative', { + input: 'fullReference', + }), + moving_average: createOperationDefinitionMock('moving_average', { input: 'fullReference', operationParams: [{ name: 'window', type: 'number', required: true }], filterable: true, getErrorMessage: jest.fn(() => ['mock error']), - // @ts-expect-error upgrade typescript v4.9.5 buildColumn: ({ referenceIds }, columnsParams) => ({ label: 'moving_average', dataType: 'number', @@ -74,7 +75,9 @@ const operationDefinitionMap: Record = { filter: getFilter(undefined, columnsParams), }), }), - cumulative_sum: createOperationDefinitionMock('cumulative_sum', { input: 'fullReference' }), + cumulative_sum: createOperationDefinitionMock('cumulative_sum', { + input: 'fullReference', + }), interval: createOperationDefinitionMock('interval', { input: 'managedReference', usedInMath: true, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts index 74d3236aa17c5..2e9c0acd41485 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts @@ -21,14 +21,14 @@ type OperationByInputType = E { input: Input } >; -export function createOperationDefinitionMock( +export function createOperationDefinitionMock( operation: string, { input = 'field', getErrorMessage, buildColumn, ...params - }: Partial = {}, + }: Partial> = {}, { label = operation, dataType = 'number', @@ -81,13 +81,11 @@ export function createOperationDefinitionMock( return { buildColumn: buildColumn ?? - jest.fn(({ referenceIds }, columnParams: PartialColumnParams) => ({ + jest.fn(({ referenceIds }, columnParams: PartialColumnParams | undefined) => ({ references: referenceIds, filter: getFilter(undefined, columnParams), ...sharedColumnParams, })), - onFieldChange: jest.fn(), - toEsAggsFn: jest.fn(), getPossibleOperation: jest.fn(() => ({ scale: 'ratio', dataType: 'number',