diff --git a/src/components/common.ts b/src/components/common.ts index c552fa0d27fe..2d6283530ab5 100644 --- a/src/components/common.ts +++ b/src/components/common.ts @@ -13,3 +13,7 @@ export type RefCallback = ( // utility types: export type Omit = Pick>; + +export function keysOf(obj: T): K[] { + return Object.keys(obj) as K[]; +} diff --git a/src/components/icon/icon.test.tsx b/src/components/icon/icon.test.tsx index 76a983e3eb68..57286bec736a 100644 --- a/src/components/icon/icon.test.tsx +++ b/src/components/icon/icon.test.tsx @@ -4,8 +4,6 @@ import { requiredProps } from '../../test/required_props'; import { EuiIcon, - IconSize, - IconType, SIZES, TYPES, } from './icon'; @@ -31,7 +29,7 @@ describe('EuiIcon', () => { }); describe('size', () => { - (SIZES as IconSize[]).forEach(size => { + SIZES.forEach(size => { test(`${size} is rendered`, () => { const component = render( @@ -43,7 +41,7 @@ describe('EuiIcon', () => { }); describe('type', () => { - (TYPES as IconType[]).forEach(type => { + TYPES.forEach(type => { test(`${type} is rendered`, () => { const component = render( diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index 2b025b07437a..691be3c082e7 100644 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -1,7 +1,7 @@ import React, { SFC, SVGAttributes } from 'react'; import classNames from 'classnames'; -import { CommonProps } from '../common'; +import { CommonProps, keysOf } from '../common'; import addDataApp from './assets/app_add_data.svg'; import advancedSettingsApp from './assets/app_advanced_settings.svg'; @@ -549,7 +549,7 @@ const typeToIconMap = { tokenFile, }; -export const TYPES = Object.keys(typeToIconMap); +export const TYPES: IconType[] = keysOf(typeToIconMap); export type IconType = keyof typeof typeToIconMap; @@ -568,7 +568,7 @@ const colorToClassMap: ColorToClassMap = { ghost: 'euiIcon--ghost', }; -export const COLORS = Object.keys(colorToClassMap); +export const COLORS: IconColor[] = keysOf(colorToClassMap); // We accept arbitrary color strings, which are impossible to type. export type IconColor = string | keyof typeof colorToClassMap; @@ -582,7 +582,7 @@ const sizeToClassNameMap = { xxl: 'euiIcon--xxLarge', }; -export const SIZES = Object.keys(sizeToClassNameMap); +export const SIZES: IconSize[] = keysOf(sizeToClassNameMap); export type IconSize = keyof typeof sizeToClassNameMap; @@ -592,7 +592,7 @@ export interface EuiIconProps { size?: IconSize; } -type Props = CommonProps & SVGAttributes & EuiIconProps; +type Props = CommonProps & SVGAttributes & EuiIconProps; export const EuiIcon: SFC = ({ type,