mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[ML] Use unknown instead of any for type guards. (#94090)
This commit is contained in:
parent
2d60d238fc
commit
066e47e9ea
13 changed files with 86 additions and 59 deletions
|
@ -35,18 +35,18 @@ const isGenericResponseSchema = <T>(arg: any): arg is T => {
|
|||
);
|
||||
};
|
||||
|
||||
export const isGetTransformsResponseSchema = (arg: any): arg is GetTransformsResponseSchema => {
|
||||
export const isGetTransformsResponseSchema = (arg: unknown): arg is GetTransformsResponseSchema => {
|
||||
return isGenericResponseSchema<GetTransformsResponseSchema>(arg);
|
||||
};
|
||||
|
||||
export const isGetTransformsStatsResponseSchema = (
|
||||
arg: any
|
||||
arg: unknown
|
||||
): arg is GetTransformsStatsResponseSchema => {
|
||||
return isGenericResponseSchema<GetTransformsStatsResponseSchema>(arg);
|
||||
};
|
||||
|
||||
export const isDeleteTransformsResponseSchema = (
|
||||
arg: any
|
||||
arg: unknown
|
||||
): arg is DeleteTransformsResponseSchema => {
|
||||
return (
|
||||
isPopulatedObject(arg) &&
|
||||
|
@ -54,26 +54,28 @@ export const isDeleteTransformsResponseSchema = (
|
|||
);
|
||||
};
|
||||
|
||||
export const isEsIndices = (arg: any): arg is EsIndex[] => {
|
||||
export const isEsIndices = (arg: unknown): arg is EsIndex[] => {
|
||||
return Array.isArray(arg);
|
||||
};
|
||||
|
||||
export const isEsSearchResponse = (arg: any): arg is SearchResponse7 => {
|
||||
export const isEsSearchResponse = (arg: unknown): arg is SearchResponse7 => {
|
||||
return isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'hits');
|
||||
};
|
||||
|
||||
export const isFieldHistogramsResponseSchema = (arg: any): arg is FieldHistogramsResponseSchema => {
|
||||
export const isFieldHistogramsResponseSchema = (
|
||||
arg: unknown
|
||||
): arg is FieldHistogramsResponseSchema => {
|
||||
return Array.isArray(arg);
|
||||
};
|
||||
|
||||
export const isGetTransformsAuditMessagesResponseSchema = (
|
||||
arg: any
|
||||
arg: unknown
|
||||
): arg is GetTransformsAuditMessagesResponseSchema => {
|
||||
return Array.isArray(arg);
|
||||
};
|
||||
|
||||
export const isPostTransformsPreviewResponseSchema = (
|
||||
arg: any
|
||||
arg: unknown
|
||||
): arg is PostTransformsPreviewResponseSchema => {
|
||||
return (
|
||||
isPopulatedObject(arg) &&
|
||||
|
@ -85,12 +87,12 @@ export const isPostTransformsPreviewResponseSchema = (
|
|||
};
|
||||
|
||||
export const isPostTransformsUpdateResponseSchema = (
|
||||
arg: any
|
||||
arg: unknown
|
||||
): arg is PostTransformsUpdateResponseSchema => {
|
||||
return isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'id') && typeof arg.id === 'string';
|
||||
};
|
||||
|
||||
export const isPutTransformsResponseSchema = (arg: any): arg is PutTransformsResponseSchema => {
|
||||
export const isPutTransformsResponseSchema = (arg: unknown): arg is PutTransformsResponseSchema => {
|
||||
return (
|
||||
isPopulatedObject(arg) &&
|
||||
{}.hasOwnProperty.call(arg, 'transformsCreated') &&
|
||||
|
@ -100,13 +102,17 @@ export const isPutTransformsResponseSchema = (arg: any): arg is PutTransformsRes
|
|||
);
|
||||
};
|
||||
|
||||
const isGenericSuccessResponseSchema = (arg: any) =>
|
||||
const isGenericSuccessResponseSchema = (arg: unknown) =>
|
||||
isPopulatedObject(arg) && Object.values(arg).every((d) => ({}.hasOwnProperty.call(d, 'success')));
|
||||
|
||||
export const isStartTransformsResponseSchema = (arg: any): arg is StartTransformsResponseSchema => {
|
||||
export const isStartTransformsResponseSchema = (
|
||||
arg: unknown
|
||||
): arg is StartTransformsResponseSchema => {
|
||||
return isGenericSuccessResponseSchema(arg);
|
||||
};
|
||||
|
||||
export const isStopTransformsResponseSchema = (arg: any): arg is StopTransformsResponseSchema => {
|
||||
export const isStopTransformsResponseSchema = (
|
||||
arg: unknown
|
||||
): arg is StopTransformsResponseSchema => {
|
||||
return isGenericSuccessResponseSchema(arg);
|
||||
};
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
import { EuiComboBoxOptionOption } from '@elastic/eui/src/components/combo_box/types';
|
||||
import type { LatestFunctionConfig, PutTransformsRequestSchema } from '../api_schemas/transforms';
|
||||
import { isPopulatedObject } from '../utils/object_utils';
|
||||
import { PivotGroupByDict } from './pivot_group_by';
|
||||
import { PivotAggDict } from './pivot_aggs';
|
||||
|
||||
|
@ -44,14 +45,12 @@ export type TransformLatestConfig = Omit<TransformBaseConfig, 'pivot'> & {
|
|||
|
||||
export type TransformConfigUnion = TransformPivotConfig | TransformLatestConfig;
|
||||
|
||||
export function isPivotTransform(
|
||||
transform: TransformBaseConfig
|
||||
): transform is TransformPivotConfig {
|
||||
return transform.hasOwnProperty('pivot');
|
||||
export function isPivotTransform(transform: unknown): transform is TransformPivotConfig {
|
||||
return isPopulatedObject(transform) && transform.hasOwnProperty('pivot');
|
||||
}
|
||||
|
||||
export function isLatestTransform(transform: any): transform is TransformLatestConfig {
|
||||
return transform.hasOwnProperty('latest');
|
||||
export function isLatestTransform(transform: unknown): transform is TransformLatestConfig {
|
||||
return isPopulatedObject(transform) && transform.hasOwnProperty('latest');
|
||||
}
|
||||
|
||||
export interface LatestFunctionConfigUI {
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import { TransformState, TRANSFORM_STATE } from '../constants';
|
||||
import { isPopulatedObject } from '../utils/object_utils';
|
||||
import { TransformId } from './transform';
|
||||
|
||||
export interface TransformStats {
|
||||
|
@ -55,11 +56,12 @@ export interface TransformStats {
|
|||
state: TransformState;
|
||||
}
|
||||
|
||||
export function isTransformStats(arg: any): arg is TransformStats {
|
||||
function isTransformState(arg: unknown): arg is TransformState {
|
||||
return typeof arg === 'string' && Object.values(TRANSFORM_STATE).includes(arg as TransformState);
|
||||
}
|
||||
|
||||
export function isTransformStats(arg: unknown): arg is TransformStats {
|
||||
return (
|
||||
typeof arg === 'object' &&
|
||||
arg !== null &&
|
||||
{}.hasOwnProperty.call(arg, 'state') &&
|
||||
Object.values(TRANSFORM_STATE).includes(arg.state)
|
||||
isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'state') && isTransformState(arg.state)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { isPopulatedObject } from './object_utils';
|
||||
|
||||
export interface ErrorResponse {
|
||||
body: {
|
||||
statusCode: number;
|
||||
|
@ -15,16 +17,16 @@ export interface ErrorResponse {
|
|||
name: string;
|
||||
}
|
||||
|
||||
export function isErrorResponse(arg: any): arg is ErrorResponse {
|
||||
return arg?.body?.error !== undefined && arg?.body?.message !== undefined;
|
||||
export function isErrorResponse(arg: unknown): arg is ErrorResponse {
|
||||
return isPopulatedObject(arg) && isPopulatedObject(arg.body) && arg?.body?.message !== undefined;
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: any) {
|
||||
export function getErrorMessage(error: unknown) {
|
||||
if (isErrorResponse(error)) {
|
||||
return `${error.body.error}: ${error.body.message}`;
|
||||
}
|
||||
|
||||
if (typeof error === 'object' && typeof error.message === 'string') {
|
||||
if (isPopulatedObject(error) && typeof error.message === 'string') {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,6 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va
|
|||
return obj;
|
||||
};
|
||||
|
||||
export const isPopulatedObject = <T = Record<string, any>>(arg: any): arg is T => {
|
||||
export const isPopulatedObject = <T = Record<string, unknown>>(arg: unknown): arg is T => {
|
||||
return typeof arg === 'object' && arg !== null && Object.keys(arg).length > 0;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue