[ML] Use unknown instead of any for type guards. (#94090)

This commit is contained in:
Walter Rafelsberger 2021-03-10 12:40:49 +01:00 committed by GitHub
parent 2d60d238fc
commit 066e47e9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 86 additions and 59 deletions

View file

@ -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);
};

View file

@ -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 {

View file

@ -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)
);
}

View file

@ -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;
}

View file

@ -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;
};

View file

@ -9,7 +9,7 @@ import { composeValidators, patternValidator } from '../../../common/shared_impo
import { AggName } from '../../../common/types/aggregations';
export function isAggName(arg: any): arg is AggName {
export function isAggName(arg: unknown): arg is AggName {
// allow all characters except `[]>` and must not start or end with a space.
const validatorFn = composeValidators(
patternValidator(/^[^\s]/),

View file

@ -14,12 +14,16 @@ import type { Dictionary } from '../../../common/types/common';
import type { EsFieldName } from '../../../common/types/fields';
import type { PivotAgg, PivotSupportedAggs } from '../../../common/types/pivot_aggs';
import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs';
import { isPopulatedObject } from '../../../common/utils/object_utils';
import { getAggFormConfig } from '../sections/create_transform/components/step_define/common/get_agg_form_config';
import { PivotAggsConfigFilter } from '../sections/create_transform/components/step_define/common/filter_agg/types';
export function isPivotSupportedAggs(arg: any): arg is PivotSupportedAggs {
return Object.values(PIVOT_SUPPORTED_AGGS).includes(arg);
export function isPivotSupportedAggs(arg: unknown): arg is PivotSupportedAggs {
return (
typeof arg === 'string' &&
Object.values(PIVOT_SUPPORTED_AGGS).includes(arg as PivotSupportedAggs)
);
}
export const PERCENTILES_AGG_DEFAULT_PERCENTS = [1, 5, 25, 50, 75, 95, 99];
@ -160,8 +164,9 @@ export type PivotAggsConfigWithUiSupport =
| PivotAggsConfigPercentiles
| PivotAggsConfigWithExtendedForm;
export function isPivotAggsConfigWithUiSupport(arg: any): arg is PivotAggsConfigWithUiSupport {
export function isPivotAggsConfigWithUiSupport(arg: unknown): arg is PivotAggsConfigWithUiSupport {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('aggName') &&
arg.hasOwnProperty('dropDownName') &&
@ -175,12 +180,13 @@ export function isPivotAggsConfigWithUiSupport(arg: any): arg is PivotAggsConfig
*/
type PivotAggsConfigWithExtendedForm = PivotAggsConfigFilter;
export function isPivotAggsWithExtendedForm(arg: any): arg is PivotAggsConfigWithExtendedForm {
return arg.hasOwnProperty('AggFormComponent');
export function isPivotAggsWithExtendedForm(arg: unknown): arg is PivotAggsConfigWithExtendedForm {
return isPopulatedObject(arg) && arg.hasOwnProperty('AggFormComponent');
}
export function isPivotAggsConfigPercentiles(arg: any): arg is PivotAggsConfigPercentiles {
export function isPivotAggsConfigPercentiles(arg: unknown): arg is PivotAggsConfigPercentiles {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('percents') &&

View file

@ -9,6 +9,7 @@ import { AggName } from '../../../common/types/aggregations';
import { Dictionary } from '../../../common/types/common';
import { EsFieldName } from '../../../common/types/fields';
import { GenericAgg } from '../../../common/types/pivot_group_by';
import { isPopulatedObject } from '../../../common/utils/object_utils';
import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/common';
import { PivotAggsConfigWithUiSupport } from './pivot_aggs';
@ -81,8 +82,9 @@ export type PivotGroupByConfig =
export type PivotGroupByConfigWithUiSupportDict = Dictionary<GroupByConfigWithUiSupport>;
export type PivotGroupByConfigDict = Dictionary<PivotGroupByConfig>;
export function isGroupByDateHistogram(arg: any): arg is GroupByDateHistogram {
export function isGroupByDateHistogram(arg: unknown): arg is GroupByDateHistogram {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('calendar_interval') &&
@ -90,8 +92,9 @@ export function isGroupByDateHistogram(arg: any): arg is GroupByDateHistogram {
);
}
export function isGroupByHistogram(arg: any): arg is GroupByHistogram {
export function isGroupByHistogram(arg: unknown): arg is GroupByHistogram {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('interval') &&
@ -99,15 +102,16 @@ export function isGroupByHistogram(arg: any): arg is GroupByHistogram {
);
}
export function isGroupByTerms(arg: any): arg is GroupByTerms {
export function isGroupByTerms(arg: unknown): arg is GroupByTerms {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.agg === PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS
);
}
export function isPivotGroupByConfigWithUiSupport(arg: any): arg is GroupByConfigWithUiSupport {
export function isPivotGroupByConfigWithUiSupport(arg: unknown): arg is GroupByConfigWithUiSupport {
return isGroupByDateHistogram(arg) || isGroupByHistogram(arg) || isGroupByTerms(arg);
}
@ -119,6 +123,6 @@ export function getEsAggFromGroupByConfig(groupByConfig: GroupByConfigBase): Gen
};
}
export function isPivotAggConfigWithUiSupport(arg: any): arg is PivotAggsConfigWithUiSupport {
return arg.hasOwnProperty('agg') && arg.hasOwnProperty('field');
export function isPivotAggConfigWithUiSupport(arg: unknown): arg is PivotAggsConfigWithUiSupport {
return isPopulatedObject(arg) && arg.hasOwnProperty('agg') && arg.hasOwnProperty('field');
}

View file

@ -58,13 +58,19 @@ export function getPivotQuery(search: string | SavedSearchQuery): PivotQuery {
return search;
}
export function isSimpleQuery(arg: any): arg is SimpleQuery {
return arg.query_string !== undefined;
export function isSimpleQuery(arg: unknown): arg is SimpleQuery {
return isPopulatedObject(arg) && arg.hasOwnProperty('query_string');
}
export const matchAllQuery = { match_all: {} };
export function isMatchAllQuery(query: any): boolean {
return query.match_all !== undefined && Object.keys(query.match_all).length === 0;
export function isMatchAllQuery(query: unknown): boolean {
return (
isPopulatedObject(query) &&
query.hasOwnProperty('match_all') &&
typeof query.match_all === 'object' &&
query.match_all !== null &&
Object.keys(query.match_all).length === 0
);
}
export const defaultQuery: PivotQuery = { query_string: { query: '*' } };
@ -94,7 +100,7 @@ export function getCombinedRuntimeMappings(
}
}
if (isPopulatedObject(combinedRuntimeMappings)) {
if (isPopulatedObject<StepDefineExposedState['runtimeMappings']>(combinedRuntimeMappings)) {
return combinedRuntimeMappings;
}
return undefined;

View file

@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import { Privileges } from '../../../../../common/types/privileges';
import { isPopulatedObject } from '../../../../../common/utils/object_utils';
export interface Capabilities {
canGetTransform: boolean;
@ -19,10 +20,9 @@ export interface Capabilities {
export type Privilege = [string, string];
function isPrivileges(arg: any): arg is Privileges {
function isPrivileges(arg: unknown): arg is Privileges {
return (
typeof arg === 'object' &&
arg !== null &&
isPopulatedObject(arg) &&
arg.hasOwnProperty('hasAllPrivileges') &&
typeof arg.hasAllPrivileges === 'boolean' &&
arg.hasOwnProperty('missingPrivileges') &&

View file

@ -72,7 +72,7 @@ export interface StepDefineExposedState {
isRuntimeMappingsEditorEnabled: boolean;
}
export function isRuntimeField(arg: any): arg is RuntimeField {
export function isRuntimeField(arg: unknown): arg is RuntimeField {
return (
isPopulatedObject(arg) &&
((Object.keys(arg).length === 1 && arg.hasOwnProperty('type')) ||
@ -84,18 +84,18 @@ export function isRuntimeField(arg: any): arg is RuntimeField {
Object.keys(arg.script).length === 1 &&
arg.script.hasOwnProperty('source') &&
typeof arg.script.source === 'string')))) &&
RUNTIME_FIELD_TYPES.includes(arg.type)
RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType)
);
}
export function isRuntimeMappings(arg: any): arg is RuntimeMappings {
export function isRuntimeMappings(arg: unknown): arg is RuntimeMappings {
return isPopulatedObject(arg) && Object.values(arg).every((d) => isRuntimeField(d));
}
export function isPivotPartialRequest(arg: any): arg is { pivot: PivotConfigDefinition } {
export function isPivotPartialRequest(arg: unknown): arg is { pivot: PivotConfigDefinition } {
return isPopulatedObject(arg) && arg.hasOwnProperty('pivot');
}
export function isLatestPartialRequest(arg: any): arg is { latest: LatestFunctionConfig } {
export function isLatestPartialRequest(arg: unknown): arg is { latest: LatestFunctionConfig } {
return isPopulatedObject(arg) && arg.hasOwnProperty('latest');
}

View file

@ -130,7 +130,7 @@ export const stringValidator: Validator = (value, isOptional = true) => {
return [];
};
function parseDurationAboveZero(arg: any, errorMessage: string): ParsedDuration | string[] {
function parseDurationAboveZero(arg: unknown, errorMessage: string): ParsedDuration | string[] {
if (typeof arg !== 'string' || arg === null) {
return [stringNotValidErrorMessage];
}

View file

@ -7,10 +7,12 @@
import { textService } from '../text';
class DocTitleService {
private changeDocTitle: any = () => {};
type ChangeDocTitle = (docTitle: string) => void;
public init(changeDocTitle: any): void {
class DocTitleService {
private changeDocTitle: ChangeDocTitle = () => {};
public init(changeDocTitle: ChangeDocTitle): void {
this.changeDocTitle = changeDocTitle;
}