From 07c725800b656a7cdef4d11cabfdb3f20f5e4bee Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Mon, 15 May 2023 09:15:26 +0100 Subject: [PATCH] Chore: some minor type fixes (#68402) * some minor type fixes * undo card change --- .betterer.results | 40 ++----------------- jest.config.js | 2 +- .../src/components/Cascader/Cascader.tsx | 2 +- .../DataSourceHttpSettings.story.tsx | 5 +-- .../DataSourceHttpSettings.tsx | 4 +- .../grafana-ui/src/components/Graph/Graph.tsx | 9 ++--- .../PluginSignatureBadge.tsx | 10 +---- public/app/types/unified-alerting.ts | 6 +-- public/test/jest-shim.ts | 15 ------- public/test/matchers/toEmitValuesWith.ts | 2 +- public/test/mocks/workers.ts | 4 +- 11 files changed, 22 insertions(+), 77 deletions(-) delete mode 100644 public/test/jest-shim.ts diff --git a/.betterer.results b/.betterer.results index 152528729fe..8960e105cc4 100644 --- a/.betterer.results +++ b/.betterer.results @@ -879,9 +879,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], - "packages/grafana-ui/src/components/Cascader/Cascader.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "packages/grafana-ui/src/components/ClickOutsideWrapper/ClickOutsideWrapper.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], @@ -902,14 +899,8 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], - "packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.story.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/components/DataSourceSettings/DataSourceHttpSettings.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "2"] + [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "0"] ], "packages/grafana-ui/src/components/DataSourceSettings/types.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -973,10 +964,7 @@ exports[`better eslint`] = { ], "packages/grafana-ui/src/components/Graph/Graph.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"], - [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"] + [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], "packages/grafana-ui/src/components/Graph/GraphContextMenu.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -1071,10 +1059,6 @@ exports[`better eslint`] = { "packages/grafana-ui/src/components/PanelChrome/index.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], - "packages/grafana-ui/src/components/PluginSignatureBadge/PluginSignatureBadge.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] - ], "packages/grafana-ui/src/components/QueryField/QueryField.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "1"] @@ -6020,11 +6004,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] ], - "public/app/types/unified-alerting.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, "Unexpected any. Specify a different type.", "2"] - ], "public/test/core/redux/mocks.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], @@ -6066,14 +6045,6 @@ exports[`better eslint`] = { "public/test/jest-setup.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], - "public/test/jest-shim.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.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"] - ], "public/test/lib/common.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], @@ -6083,16 +6054,13 @@ exports[`better eslint`] = { "public/test/matchers/toEmitValuesWith.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.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"] + [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], "public/test/matchers/utils.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/test/mocks/workers.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"] ], "public/test/specs/helpers.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/jest.config.js b/jest.config.js index 1bfdda0b359..cc8725cf2e5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -18,7 +18,7 @@ module.exports = { roots: ['/public/app', '/public/test', '/packages'], testRegex: '(\\.|/)(test)\\.(jsx?|tsx?)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], - setupFiles: ['jest-canvas-mock', './public/test/jest-shim.ts', './public/test/jest-setup.ts'], + setupFiles: ['jest-canvas-mock', './public/test/jest-setup.ts'], testTimeout: 30000, resolver: `/public/test/jest-resolver.js`, setupFilesAfterEnv: ['./public/test/setupTests.ts'], diff --git a/packages/grafana-ui/src/components/Cascader/Cascader.tsx b/packages/grafana-ui/src/components/Cascader/Cascader.tsx index 2746c39fc13..dc2be078fa2 100644 --- a/packages/grafana-ui/src/components/Cascader/Cascader.tsx +++ b/packages/grafana-ui/src/components/Cascader/Cascader.tsx @@ -49,7 +49,7 @@ export interface CascaderOption { /** * The value used under the hood */ - value: any; + value: string; /** * The label to display in the UI */ diff --git a/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.story.tsx b/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.story.tsx index 2886be9e560..8f1463efa50 100644 --- a/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.story.tsx +++ b/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.story.tsx @@ -3,12 +3,11 @@ import { useArgs } from '@storybook/client-api'; import { Meta, StoryFn } from '@storybook/react'; import React from 'react'; -import { DataSourceSettings } from '@grafana/data'; - import { DataSourceHttpSettings } from './DataSourceHttpSettings'; import mdx from './DataSourceHttpSettings.mdx'; +import { HttpSettingsProps } from './types'; -const settingsMock: DataSourceSettings = { +const settingsMock: HttpSettingsProps['dataSourceConfig'] = { id: 4, orgId: 1, uid: 'x', diff --git a/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.tsx b/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.tsx index 614e42c7c02..084fa42229b 100644 --- a/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.tsx +++ b/packages/grafana-ui/src/components/DataSourceSettings/DataSourceHttpSettings.tsx @@ -1,7 +1,7 @@ import { css, cx } from '@emotion/css'; import React, { useState, useCallback } from 'react'; -import { DataSourceSettings, SelectableValue } from '@grafana/data'; +import { SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { useTheme2 } from '../../themes'; @@ -82,7 +82,7 @@ export const DataSourceHttpSettings = (props: HttpSettingsProps) => { let urlTooltip; const onSettingsChange = useCallback( - (change: Partial>) => { + (change: Partial) => { onChange({ ...dataSourceConfig, ...change, diff --git a/packages/grafana-ui/src/components/Graph/Graph.tsx b/packages/grafana-ui/src/components/Graph/Graph.tsx index 4c08ef35603..ed20d1e4644 100644 --- a/packages/grafana-ui/src/components/Graph/Graph.tsx +++ b/packages/grafana-ui/src/components/Graph/Graph.tsx @@ -135,7 +135,7 @@ export class Graph extends PureComponent { renderTooltip = () => { const { children, series, timeZone } = this.props; const { pos, activeItem, isTooltipVisible } = this.state; - let tooltipElement: React.ReactElement | null = null; + let tooltipElement: React.ReactElement | undefined; if (!isTooltipVisible || !pos || series.length === 0) { return null; @@ -147,18 +147,17 @@ export class Graph extends PureComponent { if (tooltipElement) { return; } - // @ts-ignore const childType = c && c.type && (c.type.displayName || c.type.name); if (childType === VizTooltip.displayName) { - tooltipElement = c as React.ReactElement; + tooltipElement = c; } }); // If no tooltip provided, skip rendering if (!tooltipElement) { return null; } - const tooltipElementProps = (tooltipElement as React.ReactElement).props; + const tooltipElementProps = tooltipElement.props; const tooltipMode = tooltipElementProps.mode || 'single'; @@ -203,7 +202,7 @@ export class Graph extends PureComponent { const tooltipContent = React.createElement(tooltipContentRenderer, { ...tooltipContentProps }); - return React.cloneElement(tooltipElement as React.ReactElement, { + return React.cloneElement(tooltipElement, { content: tooltipContent, position: { x: pos.pageX, y: pos.pageY }, offset: { x: 10, y: 10 }, diff --git a/packages/grafana-ui/src/components/PluginSignatureBadge/PluginSignatureBadge.tsx b/packages/grafana-ui/src/components/PluginSignatureBadge/PluginSignatureBadge.tsx index 7992353cf66..7cb452c3340 100644 --- a/packages/grafana-ui/src/components/PluginSignatureBadge/PluginSignatureBadge.tsx +++ b/packages/grafana-ui/src/components/PluginSignatureBadge/PluginSignatureBadge.tsx @@ -14,16 +14,10 @@ export interface PluginSignatureBadgeProps extends HTMLAttributes { +export const PluginSignatureBadge = ({ status, color, ...otherProps }: PluginSignatureBadgeProps) => { const display = getSignatureDisplayModel(status); return ( - + ); }; diff --git a/public/app/types/unified-alerting.ts b/public/app/types/unified-alerting.ts index cc160c87336..7a9525f3a11 100644 --- a/public/app/types/unified-alerting.ts +++ b/public/app/types/unified-alerting.ts @@ -23,7 +23,7 @@ export type Alert = { }; export function hasAlertState(alert: Alert, state: PromAlertingRuleState | GrafanaAlertState): boolean { - return mapStateWithReasonToBaseState(alert.state as GrafanaAlertStateWithReason) === state; + return mapStateWithReasonToBaseState(alert.state) === state; } interface RuleBase { @@ -185,7 +185,7 @@ export interface SilenceFilterState { interface EvalMatch { metric: string; - tags?: any; + tags?: Record; value: number; } @@ -208,7 +208,7 @@ export interface StateHistoryItem { time: number; timeEnd: number; text: string; - tags: any[]; + tags: string[]; login: string; email: string; avatarUrl: string; diff --git a/public/test/jest-shim.ts b/public/test/jest-shim.ts deleted file mode 100644 index 5b01becaaae..00000000000 --- a/public/test/jest-shim.ts +++ /dev/null @@ -1,15 +0,0 @@ -(global as any).requestAnimationFrame = (callback: any) => { - setTimeout(callback, 0); -}; - -(Promise.prototype as any).finally = function (onFinally: any) { - return this.then( - /* onFulfilled */ - (res: any) => Promise.resolve(onFinally()).then(() => res), - /* onRejected */ - (err: any) => - Promise.resolve(onFinally()).then(() => { - throw err; - }) - ); -}; diff --git a/public/test/matchers/toEmitValuesWith.ts b/public/test/matchers/toEmitValuesWith.ts index 0764e674b65..d9a53e965a0 100644 --- a/public/test/matchers/toEmitValuesWith.ts +++ b/public/test/matchers/toEmitValuesWith.ts @@ -3,7 +3,7 @@ import { Observable, Subscription } from 'rxjs'; import { expectObservable, forceObservableCompletion } from './utils'; -function tryExpectations(received: any[], expectations: (received: any[]) => void): jest.CustomMatcherResult { +function tryExpectations(received: unknown[], expectations: (received: unknown[]) => void): jest.CustomMatcherResult { try { expectations(received); return { diff --git a/public/test/mocks/workers.ts b/public/test/mocks/workers.ts index 20aa32146bc..2b7802a4a1f 100644 --- a/public/test/mocks/workers.ts +++ b/public/test/mocks/workers.ts @@ -5,12 +5,12 @@ class LayoutMockWorker { constructor() {} postMessage(data: any) { const { nodes, edges, config } = data; - this.timeout = setTimeout(() => { + this.timeout = window.setTimeout(() => { this.timeout = undefined; layout(nodes, edges, config); // @ts-ignore this.onmessage({ data: { nodes, edges } }); - }, 1) as any; + }, 1); } terminate() { if (this.timeout) {