Skip to content

Commit 6f830cc

Browse files
authored
Refactor 'any' types to strict type definitions across codebase (#249)
1 parent b62f00d commit 6f830cc

File tree

4 files changed

+60
-44
lines changed

4 files changed

+60
-44
lines changed

example/src/App.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
SkiaChart,
1010
} from '@wuba/react-native-echarts';
1111
import * as echarts from 'echarts/core';
12+
import type { EChartsOption } from 'echarts';
13+
import type { ChartElement } from '@wuba/react-native-echarts';
1214
import { BarChart, LineChart } from 'echarts/charts';
1315
import {
1416
TitleComponent,
@@ -31,11 +33,11 @@ echarts.use([
3133
const E_HEIGHT = 250;
3234
const E_WIDTH = Dimensions.get('screen').width;
3335

34-
function SkiaComponent({ option }: any) {
35-
const skiaRef = useRef<any>(null);
36+
function SkiaComponent({ option }: { option: EChartsOption }) {
37+
const skiaRef = useRef<(ChartElement & any) | null>(null);
3638

3739
useEffect(() => {
38-
let chart: any;
40+
let chart: echarts.EChartsType | undefined;
3941
if (skiaRef.current) {
4042
chart = echarts.init(skiaRef.current, 'light', {
4143
// @ts-ignore
@@ -51,11 +53,11 @@ function SkiaComponent({ option }: any) {
5153
return <SkiaChart ref={skiaRef} />;
5254
}
5355

54-
function SvgComponent({ option }: any) {
55-
const svgRef = useRef<any>(null);
56+
function SvgComponent({ option }: { option: EChartsOption }) {
57+
const svgRef = useRef<(ChartElement & any) | null>(null);
5658

5759
useEffect(() => {
58-
let chart: any;
60+
let chart: echarts.EChartsType | undefined;
5961
if (svgRef.current) {
6062
chart = echarts.init(svgRef.current, 'light', {
6163
renderer: 'svg',
@@ -75,29 +77,29 @@ const fontFamily = Platform.select({
7577
});
7678
const option = {
7779
xAxis: {
78-
type: 'category',
80+
type: 'category' as const,
7981
data: ['周一', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
8082
},
8183
yAxis: {
82-
type: 'value',
84+
type: 'value' as const,
8385
},
8486
tooltip: {
85-
trigger: 'axis',
87+
trigger: 'axis' as const,
8688
},
8789
series: [
8890
{
8991
data: [120, 200, 150, 80, 70, 110, 130],
90-
type: 'bar',
92+
type: 'bar' as const,
9193
},
9294
{
9395
data: [220, 100, 180, 160, 150, 120, 110],
94-
type: 'line',
96+
type: 'line' as const,
9597
},
9698
],
9799
textStyle: {
98100
fontFamily,
99101
},
100-
};
102+
} as EChartsOption;
101103
function App() {
102104
return (
103105
<View style={styles.container}>

src/__tests__/chart.test.tsx

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import SVGComponent from '../svg/svgChart';
1717
import { dispatchEventsToZRender } from '../components/events';
1818
import { SVGRenderer } from '../svg/SVGRenderer';
1919
import * as echarts from 'echarts/core';
20+
import type { EChartsType } from 'echarts/core';
21+
import type {
22+
RNGestureHandlerGesture,
23+
ChartElement,
24+
DefaultRNGestures,
25+
DispatchEvents,
26+
} from '../types';
2027
import { BarChart, LineChart, GraphChart, ScatterChart } from 'echarts/charts';
2128
import {
2229
PanGesture,
@@ -108,7 +115,7 @@ function getOptionTwo() {
108115
animation: false,
109116
tooltip: {},
110117
animationDurationUpdate: 1500,
111-
animationEasingUpdate: 'quinticInOut',
118+
animationEasingUpdate: 'quinticInOut' as any, // Temporarily cast to any for test
112119
series: [
113120
{
114121
type: 'graph',
@@ -244,8 +251,8 @@ function zoomElement(pan: ReactTestInstance, x: number, y: number) {
244251
}
245252

246253
interface ChartCall {
247-
call: (chart: any) => void;
248-
snapshot?: (data: string) => any;
254+
call: (chart: EChartsType) => void;
255+
snapshot?: (data: string) => Promise<Buffer>;
249256
}
250257

251258
function Chart({
@@ -255,15 +262,15 @@ function Chart({
255262
gesture,
256263
handleGesture = true,
257264
}: {
258-
Component: React.ComponentType<any>;
265+
Component: React.ComponentType<any>; // Keep as any to allow both SkiaChartProps and SVGChartProps with ref
259266
calls?: ChartCall[];
260267
useRNGH?: boolean;
261-
gesture?: any;
268+
gesture?: RNGestureHandlerGesture;
262269
handleGesture?: boolean;
263270
}) {
264-
const ref = useRef<any>(null);
271+
const ref = useRef<(ChartElement & any) | null>(null);
265272
useEffect(() => {
266-
let chart: any;
273+
let chart: EChartsType | undefined;
267274
if (ref.current) {
268275
// @ts-ignore
269276
chart = echarts.init(ref.current, 'light', {
@@ -273,17 +280,19 @@ function Chart({
273280
});
274281
(async () => {
275282
for (const call of calls) {
276-
call.call(chart);
277-
if (call.snapshot) {
278-
await new Promise((resolve) =>
279-
setTimeout(async () => {
280-
const result = call.snapshot?.(chart.getDataURL());
281-
if (result instanceof Promise) {
282-
await result;
283-
}
284-
resolve(undefined);
285-
}, 0)
286-
);
283+
if (chart) {
284+
call.call(chart);
285+
if (call.snapshot) {
286+
await new Promise((resolve) =>
287+
setTimeout(async () => {
288+
const result = call.snapshot?.(chart!.getDataURL());
289+
if (result instanceof Promise) {
290+
await result;
291+
}
292+
resolve(undefined);
293+
}, 0)
294+
);
295+
}
287296
}
288297
}
289298
})();
@@ -314,7 +323,7 @@ Components.forEach((Component) => {
314323
Component={Component}
315324
calls={[
316325
{
317-
call: (chart: any) => {
326+
call: (chart: EChartsType) => {
318327
chart.setOption(getOption('line'));
319328
},
320329
snapshot,
@@ -333,13 +342,13 @@ Components.forEach((Component) => {
333342
<Chart
334343
calls={[
335344
{
336-
call: (chart: any) => {
345+
call: (chart: EChartsType) => {
337346
act(() => chart.setOption(getOption()));
338347
},
339348
snapshot,
340349
},
341350
{
342-
call: (chart: any) => {
351+
call: (chart: EChartsType) => {
343352
const id = chart.getZr().id;
344353
act(() =>
345354
dispatchEventsToZRender(
@@ -373,7 +382,7 @@ Components.forEach((Component) => {
373382
handleGesture={false}
374383
calls={[
375384
{
376-
call: (chart: any) => {
385+
call: (chart: EChartsType) => {
377386
chart.setOption(getOption('scatter'));
378387
},
379388
snapshot,
@@ -393,7 +402,7 @@ Components.forEach((Component) => {
393402
Component={Component}
394403
calls={[
395404
{
396-
call: (chart: any) => {
405+
call: (chart: EChartsType) => {
397406
chart.setOption(getOptionTwo());
398407
},
399408
snapshot,
@@ -435,7 +444,7 @@ Components.forEach((Component) => {
435444
useRNGH
436445
calls={[
437446
{
438-
call: (chart: any) => {
447+
call: (chart: EChartsType) => {
439448
chart.setOption(getOptionTwo());
440449
},
441450
snapshot,
@@ -495,9 +504,12 @@ Components.forEach((Component) => {
495504
<RNGHChart
496505
Component={Component}
497506
useRNGH
498-
gesture={(...args: any) => {
499-
gesture(...args);
500-
return args[0];
507+
gesture={(
508+
defaultGestures: DefaultRNGestures,
509+
dispatchEvents: DispatchEvents
510+
) => {
511+
gesture(defaultGestures, dispatchEvents);
512+
return defaultGestures[0];
501513
}}
502514
/>
503515
);

src/skia/graphic.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ import {
4343
} from '@shopify/react-native-skia';
4444
import React from 'react';
4545

46-
function isImageLike(val: any): val is HTMLImageElement {
47-
return val && isString(val.src);
46+
function isImageLike(val: unknown): val is HTMLImageElement {
47+
return val != null && typeof val === 'object' && isString((val as any).src);
4848
}
49-
function isCanvasLike(val: any): val is HTMLCanvasElement {
50-
return val && isFunction(val.toDataURL);
49+
function isCanvasLike(val: unknown): val is HTMLCanvasElement {
50+
return (
51+
val != null && typeof val === 'object' && isFunction((val as any).toDataURL)
52+
);
5153
}
5254

5355
type SVGVNodeAttrs = Record<

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type HandlerName =
2121
export type DispatchEvents = (
2222
types: HandlerName[],
2323
nativeEvent: any,
24-
eventArgs?: any
24+
eventArgs?: Record<string, unknown>
2525
) => void;
2626

2727
export type DefaultRNGestures = [PanGesture, PinchGesture, TapGesture];

0 commit comments

Comments
 (0)