From 7ac7f844f4cd2f6d2a053b5e30a9f5c86920ac6b Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Wed, 5 Oct 2022 09:04:10 +0100 Subject: [PATCH] improve some grafana-ui types (#56318) --- .betterer.results | 25 +------------------ .../components/FilterInput/FilterInput.tsx | 4 +-- packages/grafana-ui/src/utils/reactUtils.ts | 2 +- .../utils/storybook/ThemedDocsContainer.tsx | 4 +-- packages/grafana-ui/src/utils/table.ts | 2 +- .../grafana-ui/src/utils/useCombinedRefs.ts | 8 +++--- .../grafana-ui/src/utils/useDelayedSwitch.ts | 6 ++--- packages/grafana-ui/src/utils/validate.ts | 4 +-- 8 files changed, 17 insertions(+), 38 deletions(-) diff --git a/.betterer.results b/.betterer.results index af83c521777..60ccbf9f014 100644 --- a/.betterer.results +++ b/.betterer.results @@ -1279,9 +1279,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] ], - "packages/grafana-ui/src/components/FilterInput/FilterInput.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "packages/grafana-ui/src/components/FormattedValueDisplay/FormattedValueDisplay.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], @@ -1887,36 +1884,16 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], - "packages/grafana-ui/src/utils/reactUtils.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], - "packages/grafana-ui/src/utils/storybook/ThemedDocsContainer.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "packages/grafana-ui/src/utils/storybook/withTheme.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], "packages/grafana-ui/src/utils/table.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "packages/grafana-ui/src/utils/useAsyncDependency.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], - "packages/grafana-ui/src/utils/useCombinedRefs.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] - ], - "packages/grafana-ui/src/utils/useDelayedSwitch.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"] - ], - "packages/grafana-ui/src/utils/validate.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "packages/jaeger-ui-components/src/ScrollManager.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], diff --git a/packages/grafana-ui/src/components/FilterInput/FilterInput.tsx b/packages/grafana-ui/src/components/FilterInput/FilterInput.tsx index e303aa70dd6..403df7146c6 100644 --- a/packages/grafana-ui/src/components/FilterInput/FilterInput.tsx +++ b/packages/grafana-ui/src/components/FilterInput/FilterInput.tsx @@ -16,8 +16,8 @@ export interface Props extends Omit, 'onChange'> { export const FilterInput = React.forwardRef( ({ value, width, onChange, escapeRegex = true, ...restProps }, ref) => { - const innerRef = React.useRef(null); - const combinedRef = useCombinedRefs(ref, innerRef) as React.Ref; + const innerRef = React.useRef(null); + const combinedRef = useCombinedRefs(ref, innerRef); const suffix = value !== '' ? ( diff --git a/packages/grafana-ui/src/utils/reactUtils.ts b/packages/grafana-ui/src/utils/reactUtils.ts index 33d9c8ed5a1..9c4766b4eeb 100644 --- a/packages/grafana-ui/src/utils/reactUtils.ts +++ b/packages/grafana-ui/src/utils/reactUtils.ts @@ -24,7 +24,7 @@ export function getChildId(children: ReactElement): string | undefined { * @param itemToRender * @param props props to be passed to the function if item provided as such */ -export function renderOrCallToRender( +export function renderOrCallToRender( itemToRender: ((props?: TProps) => React.ReactNode) | React.ReactNode, props?: TProps ): React.ReactNode { diff --git a/packages/grafana-ui/src/utils/storybook/ThemedDocsContainer.tsx b/packages/grafana-ui/src/utils/storybook/ThemedDocsContainer.tsx index 32f2773fbfa..e6896beeccd 100644 --- a/packages/grafana-ui/src/utils/storybook/ThemedDocsContainer.tsx +++ b/packages/grafana-ui/src/utils/storybook/ThemedDocsContainer.tsx @@ -1,13 +1,13 @@ // This is a temporary workaround to allow theme switching storybook docs // see https://github.com/storybookjs/storybook/issues/10523 for further details -import { DocsContainer } from '@storybook/addon-docs'; +import { DocsContainer, DocsContextProps } from '@storybook/addon-docs'; import React from 'react'; import { useDarkMode } from 'storybook-dark-mode'; import { GrafanaLight, GrafanaDark } from '../../../.storybook/storybookTheme'; type Props = { - context: any; + context: DocsContextProps; }; export const ThemedDocsContainer: React.FC = ({ children, context }) => { diff --git a/packages/grafana-ui/src/utils/table.ts b/packages/grafana-ui/src/utils/table.ts index 1fb03a6aa1b..42856f7200a 100644 --- a/packages/grafana-ui/src/utils/table.ts +++ b/packages/grafana-ui/src/utils/table.ts @@ -6,7 +6,7 @@ import { Field, LinkModel } from '@grafana/data'; * @internal */ export const getCellLinks = (field: Field, row: Row) => { - let links: Array> | undefined; + let links: Array> | undefined; if (field.getLinks) { links = field.getLinks({ valueRowIndex: row.index, diff --git a/packages/grafana-ui/src/utils/useCombinedRefs.ts b/packages/grafana-ui/src/utils/useCombinedRefs.ts index e158a9630db..4070edb3fc0 100644 --- a/packages/grafana-ui/src/utils/useCombinedRefs.ts +++ b/packages/grafana-ui/src/utils/useCombinedRefs.ts @@ -1,10 +1,12 @@ import React from 'react'; -export function useCombinedRefs(...refs: any) { - const targetRef = React.useRef(null); +export function useCombinedRefs( + ...refs: Array | React.ForwardedRef | ((instance: T | null) => void)> +) { + const targetRef = React.useRef(null); React.useEffect(() => { - refs.forEach((ref: any) => { + refs.forEach((ref) => { if (!ref) { return; } diff --git a/packages/grafana-ui/src/utils/useDelayedSwitch.ts b/packages/grafana-ui/src/utils/useDelayedSwitch.ts index 4a91ca11eba..46c865bd326 100644 --- a/packages/grafana-ui/src/utils/useDelayedSwitch.ts +++ b/packages/grafana-ui/src/utils/useDelayedSwitch.ts @@ -22,13 +22,13 @@ export function useDelayedSwitch(value: boolean, options: DelayOptions = {}): bo const onStartTime = useRef(); useEffect(() => { - let timeout: number | undefined; + let timeout: ReturnType | undefined; if (value) { // If toggling to "on" state we always setTimeout no matter how long we have been "off". timeout = setTimeout(() => { onStartTime.current = new Date(); setDelayedValue(value); - }, delay) as any; + }, delay); } else { // If toggling to "off" state we check how much time we were already "on". const timeSpent = onStartTime.current ? Date.now() - onStartTime.current.valueOf() : 0; @@ -40,7 +40,7 @@ export function useDelayedSwitch(value: boolean, options: DelayOptions = {}): bo // We already spent enough time "on" so change right away. turnOff(); } else { - timeout = setTimeout(turnOff, duration - timeSpent) as any; + timeout = setTimeout(turnOff, duration - timeSpent); } } return () => { diff --git a/packages/grafana-ui/src/utils/validate.ts b/packages/grafana-ui/src/utils/validate.ts index a40b2ec7df0..d4751a21e01 100644 --- a/packages/grafana-ui/src/utils/validate.ts +++ b/packages/grafana-ui/src/utils/validate.ts @@ -7,12 +7,12 @@ export enum EventsWithValidation { } export const validate = (value: string, validationRules: ValidationRule[]) => { - const errors = validationRules.reduce((acc, currRule) => { + const errors = validationRules.reduce((acc, currRule) => { if (!currRule.rule(value)) { return acc.concat(currRule.errorMessage); } return acc; - }, [] as string[]); + }, []); return errors.length > 0 ? errors : null; };