Skip to content

Commit

Permalink
replaces any with unknown, adds test to show how optional chaining of…
Browse files Browse the repository at this point in the history
… array items can result in undefined even though typescript says its not
  • Loading branch information
dhurley14 committed Feb 27, 2020
1 parent d7bb1bc commit e796f1a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export const createFindRulesStatusRoute = (getClients: GetScopedClients): Hapi.S
searchFields: ['alertId'],
});
const accumulated = await acc;

// Array accessors can result in undefined but
// this is not represented in typescript for some reason,
// https://github.com/Microsoft/TypeScript/issues/11122
const currentStatus = convertToSnakeCase<IRuleStatusAttributes>(
lastFiveErrorsForId.saved_objects[0]?.attributes
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import Boom from 'boom';

import { SavedObjectsFindResponse } from 'kibana/server';
import { IRuleSavedAttributesSavedObjectAttributes, IRuleStatusAttributes } from '../rules/types';
import {
transformError,
transformBulkError,
Expand Down Expand Up @@ -323,5 +325,19 @@ describe('utils', () => {
const values = {};
expect(convertToSnakeCase(values)).toEqual({});
});
it('returns null when passed in undefined', () => {
// Array accessors can result in undefined but
// this is not represented in typescript for some reason,
// https://github.com/Microsoft/TypeScript/issues/11122
const values: SavedObjectsFindResponse<IRuleSavedAttributesSavedObjectAttributes> = {
page: 0,
per_page: 5,
total: 0,
saved_objects: [],
};
expect(
convertToSnakeCase<IRuleStatusAttributes>(values.saved_objects[0]?.attributes) // this is undefined, but it says it's not
).toEqual(null);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ export const getIndex = (getSpaceId: () => string, config: LegacyServices['confi
return `${signalsIndex}-${spaceId}`;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const convertToSnakeCase = <T extends Record<string, any>>(obj: T): Partial<T> | null => {
export const convertToSnakeCase = <T extends Record<string, unknown>>(
obj: T
): Partial<T> | null => {
if (!obj) {
return null;
}
Expand Down

0 comments on commit e796f1a

Please sign in to comment.