Skip to content

Commit 5d01fe8

Browse files
authored
refactor: cleanup types (#255)
## Motivation removes all instances of lazy typing (`any`)
1 parent 84f43d9 commit 5d01fe8

File tree

14 files changed

+300
-104
lines changed

14 files changed

+300
-104
lines changed

jest.setup.client.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import "@testing-library/jest-dom";
1+
// Define an interface for the mock if TypeScript doesn't include CSS or is missing properties/methods you need
2+
interface CSSMock {
3+
escape: (input: string) => string;
4+
}
25

6+
// Assigning the mock to global.CSS with proper typing
37
if (typeof CSS === "undefined") {
4-
global.CSS = {
8+
const mockCSS: CSSMock = {
59
escape: (string_: string) => string_.replaceAll(/([()\\{}])/g, "\\$1"),
6-
} as any; // Cast to 'any' to bypass TypeScript's type checking for the mock.
10+
};
11+
12+
// Extend the existing global interface (if necessary) and assign the mock
13+
global.CSS = mockCSS as typeof CSS;
714
}

package-lock.json

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/apps/story/components.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import Box from "@mui/joy/Box";
2121
import Button from "@mui/joy/Button";
2222
import Sheet from "@mui/joy/Sheet";
2323
import { useAtom } from "jotai/index";
24-
import type { CSSProperties } from "react";
24+
import type { CSSProperties, RefCallback } from "react";
2525
import { useMemo } from "react";
2626
import { useState } from "react";
2727
import { forwardRef, type LegacyRef, type ReactNode, useCallback, useRef } from "react";
28+
import type { ScrollbarProps } from "react-custom-scrollbars";
2829
import Scrollbars from "react-custom-scrollbars";
2930
import AutoSizer from "react-virtualized-auto-sizer";
3031
import { FixedSizeGrid } from "react-window";
@@ -172,13 +173,13 @@ export function LegacyCustomScrollbars({
172173
style,
173174
children,
174175
}: {
175-
onScroll?: any;
176-
forwardedRef?: any;
177-
style?: any;
178-
children?: any;
176+
onScroll?: ScrollbarProps["onScroll"];
177+
forwardedRef?: RefCallback<HTMLDivElement>;
178+
style?: CSSProperties;
179+
children?: ReactNode;
179180
}) {
180-
const referenceSetter: LegacyRef<any> = useCallback(
181-
(scrollbarsReference: { view: any }) => {
181+
const referenceSetter: LegacyRef<unknown> = useCallback(
182+
(scrollbarsReference: { view: HTMLDivElement }) => {
182183
if (forwardedRef) {
183184
if (scrollbarsReference) {
184185
forwardedRef(scrollbarsReference.view);
@@ -214,10 +215,14 @@ export function LegacyCustomScrollbars({
214215
);
215216
}
216217

217-
export const CustomScrollbarsVirtualList = forwardRef<
218-
HTMLDivElement,
219-
{ onScroll: any; forwardedRef: any; style: any; children: any }
220-
>((properties, reference) => <LegacyCustomScrollbars {...properties} forwardedRef={reference} />);
218+
export const CustomScrollbarsVirtualList = forwardRef<HTMLDivElement | undefined>(
219+
(properties, reference) => (
220+
<LegacyCustomScrollbars
221+
{...properties}
222+
forwardedRef={reference as RefCallback<HTMLDivElement>}
223+
/>
224+
)
225+
);
221226

222227
CustomScrollbarsVirtualList.displayName = "CustomScrollbarsVirtualList";
223228

src/client/atoms/icons/dynamic.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import type { SvgIconProps } from "@mui/joy/SvgIcon";
1+
import type { DefaultComponentProps } from "@mui/material/OverridableComponent";
2+
import type { SvgIconProps } from "@mui/material/SvgIcon";
3+
import type { SvgIconTypeMap } from "@mui/material/SvgIcon";
24
import dynamic from "next/dynamic";
5+
import type { ComponentType } from "react";
36
import { createElement } from "react";
47

58
const Brush = dynamic(() => import("@mui/icons-material/Brush"));
@@ -17,7 +20,10 @@ const ShoppingBag = dynamic(() => import("@mui/icons-material/ShoppingBag"));
1720
const Stream = dynamic(() => import("@mui/icons-material/Stream"));
1821
const QuestionMark = dynamic(() => import("@mui/icons-material/QuestionMark"));
1922

20-
const iconCache: Record<string, any> = {
23+
const iconCache: Record<
24+
string,
25+
ComponentType<DefaultComponentProps<SvgIconTypeMap<object, "svg">>>
26+
> = {
2127
Brush,
2228
DarkMode,
2329
Dashboard,
@@ -39,5 +45,5 @@ const iconCache: Record<string, any> = {
3945

4046
export function DynamicIcon({ icon, ...rest }: { icon: string } & SvgIconProps) {
4147
const component = iconCache[icon] ?? null;
42-
return component && createElement(component, rest);
48+
return component && createElement(component, rest as SvgIconProps);
4349
}

src/client/ions/handlers/__tests__/action.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { VectorStoreResponse } from "@captn/utils/types";
1+
import type { IPCHandlers, VectorStoreResponse } from "@captn/utils/types";
22

33
import { handleCaptainAction } from "../action";
44

@@ -10,11 +10,9 @@ jest.mock("#/build-key", () => ({
1010
}));
1111

1212
describe("handleCaptainAction", () => {
13-
// Mocking window.ipc.send
1413
const mockSend = jest.fn();
1514
beforeAll(() => {
16-
// Ensure window.ipc exists
17-
global.window.ipc = { send: mockSend } as any;
15+
global.window.ipc = { send: mockSend } as unknown as IPCHandlers;
1816
});
1917

2018
beforeEach(() => {

src/electron/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const handlers = {
6464
send(channel: string, value?: unknown) {
6565
ipcRenderer.send(channel, value);
6666
},
67-
on(channel: string, callback: (...arguments_: any[]) => void) {
68-
function subscription(_event: IpcRendererEvent, ...arguments_: any[]) {
67+
on(channel: string, callback: (...arguments_: unknown[]) => void) {
68+
function subscription(_event: IpcRendererEvent, ...arguments_: unknown[]) {
6969
return callback(...arguments_);
7070
}
7171

src/electron/helpers/langchain/__tests__/custom-hugging-face-transformers-embeddings.test.e2e.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import path from "node:path";
22

3+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4+
// @ts-ignore
35
import { env } from "@xenova/transformers";
46

7+
import { CustomHuggingFaceTransformersEmbeddings } from "../custom-hugging-face-transformers-embeddings";
8+
59
const originalImplementation = Array.isArray;
610
// @ts-expect-error we just want to mock this
711
Array.isArray = jest.fn(type => {
@@ -16,10 +20,8 @@ Array.isArray = jest.fn(type => {
1620
return originalImplementation(type);
1721
});
1822

19-
import { CustomHuggingFaceTransformersEmbeddings } from "../custom-hugging-face-transformers-embeddings";
20-
2123
describe("CustomHuggingFaceEmbeddings", () => {
22-
let embeddings: any;
24+
let embeddings: CustomHuggingFaceTransformersEmbeddings;
2325

2426
beforeAll(() => {
2527
env.localModelPath = path.join(process.cwd(), "models");
@@ -48,14 +50,14 @@ describe("CustomHuggingFaceEmbeddings", () => {
4850
expect(results.length).toEqual(texts.length);
4951

5052
// Check each result to ensure it's an array and not empty
51-
for (const [index, embedding] of results.entries()) {
53+
for (const [, embedding] of results.entries()) {
5254
expect(Array.isArray(embedding)).toBeTruthy();
5355
expect(embedding.length).toBeGreaterThan(0);
5456
}
5557
});
5658

5759
describe("without maxTokens defined", () => {
58-
let defaultEmbeddings: any;
60+
let defaultEmbeddings: CustomHuggingFaceTransformersEmbeddings;
5961

6062
beforeAll(() => {
6163
defaultEmbeddings = new CustomHuggingFaceTransformersEmbeddings({
@@ -79,7 +81,7 @@ describe("CustomHuggingFaceEmbeddings", () => {
7981
expect(Array.isArray(results)).toBeTruthy();
8082
expect(results.length).toEqual(texts.length);
8183

82-
for (const [index, embedding] of results.entries()) {
84+
for (const [, embedding] of results.entries()) {
8385
expect(Array.isArray(embedding)).toBeTruthy();
8486
expect(embedding.length).toBeGreaterThan(0);
8587
}

src/electron/helpers/services/__tests__/download-manager.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { unpack } from "@/utils/unpack";
1616

1717
const testDownloadFile = "https://example.com/test.jpg";
1818

19+
interface DownloadEventArguments {
20+
action: DownloadEvent;
21+
}
22+
1923
jest.mock("electron", () => {
2024
const originalModule = jest.requireActual("electron");
2125

@@ -377,11 +381,16 @@ describe("DownloadManager", () => {
377381
});
378382

379383
expect(
380-
spySend.mock.calls.some((call: any) => call[1].action === DownloadEvent.PROGRESS)
384+
spySend.mock.calls.some(
385+
(call: [string, unknown]) =>
386+
(call[1] as DownloadEventArguments).action === DownloadEvent.PROGRESS
387+
)
381388
).toBe(true);
382389
expect(
383-
spySend.mock.calls.filter((call: any) => call[1].action === DownloadEvent.PROGRESS)
384-
.length
390+
spySend.mock.calls.filter(
391+
(call: [string, unknown]) =>
392+
(call[1] as DownloadEventArguments).action === DownloadEvent.PROGRESS
393+
).length
385394
).toBeGreaterThan(1);
386395
});
387396

0 commit comments

Comments
 (0)