Skip to content

Commit bdf140b

Browse files
committed
refactor: remove any
1 parent 05416d5 commit bdf140b

9 files changed

Lines changed: 116 additions & 64 deletions

File tree

‎.oxlintrc.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"$schema": "./node_modules/oxlint/configuration_schema.json",
33
"rules": {
4-
"sort-imports": "off"
4+
"sort-imports": "off",
5+
"@typescript-eslint/no-explicit-any": "error"
56
}
67
}

‎packages/client/src/client.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ export class TypedClient<TContract extends ContractDefinition> {
193193
for (const [queryName, queryDef] of Object.entries(definition.queries ?? {}) as Array<
194194
[string, QueryDefinition]
195195
>) {
196-
(queries as any)[queryName] = async (args: ClientInferInput<typeof queryDef>) => {
196+
(queries as Record<string, unknown>)[queryName] = async (
197+
args: ClientInferInput<typeof queryDef>,
198+
) => {
197199
const validatedInput = queryDef.input.parse(args);
198200
const result = await handle.query(queryName as string, validatedInput);
199201
return queryDef.output.parse(result);
@@ -205,7 +207,9 @@ export class TypedClient<TContract extends ContractDefinition> {
205207
for (const [signalName, signalDef] of Object.entries(definition.signals ?? {}) as Array<
206208
[string, SignalDefinition]
207209
>) {
208-
(signals as any)[signalName] = async (args: ClientInferInput<typeof signalDef>) => {
210+
(signals as Record<string, unknown>)[signalName] = async (
211+
args: ClientInferInput<typeof signalDef>,
212+
) => {
209213
const validatedInput = signalDef.input.parse(args);
210214
await handle.signal(signalName as string, validatedInput);
211215
};
@@ -216,7 +220,9 @@ export class TypedClient<TContract extends ContractDefinition> {
216220
for (const [updateName, updateDef] of Object.entries(definition.updates ?? {}) as Array<
217221
[string, UpdateDefinition]
218222
>) {
219-
(updates as any)[updateName] = async (args: ClientInferInput<typeof updateDef>) => {
223+
(updates as Record<string, unknown>)[updateName] = async (
224+
args: ClientInferInput<typeof updateDef>,
225+
) => {
220226
const validatedInput = updateDef.input.parse(args);
221227
const result = await handle.executeUpdate(updateName as string, { args: [validatedInput] });
222228
return updateDef.output.parse(result);

‎packages/contract/src/types.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ describe("Core Types", () => {
225225
output: z.object({ sent: z.boolean() }),
226226
},
227227
},
228-
} satisfies ContractDefinition<any, any>;
228+
} satisfies ContractDefinition;
229229

230230
expect(contractDef.taskQueue).toBe("test-queue");
231231
expect(contractDef.workflows).toBeDefined();
@@ -241,11 +241,11 @@ describe("Core Types", () => {
241241
output: z.object({ result: z.string() }),
242242
},
243243
},
244-
} satisfies ContractDefinition<any, any>;
244+
} satisfies ContractDefinition;
245245

246246
expect(contractDef.taskQueue).toBe("test-queue");
247247
expect(contractDef.workflows).toBeDefined();
248-
expect((contractDef as any).activities).toBeUndefined();
248+
expect((contractDef as { activities?: unknown }).activities).toBeUndefined();
249249
});
250250
});
251251

‎packages/contract/src/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { z } from "zod";
33
/**
44
* Base types for validation schemas
55
*/
6-
export type AnyZodSchema = z.ZodTypeAny;
6+
export type AnyZodSchema = z.ZodType;
77

88
/**
99
* Definition of an activity

‎packages/worker-boxed/src/handler.spec.ts‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { describe, expect, it } from "vitest";
22
import { Future, Result } from "@swan-io/boxed";
33
import { z } from "zod";
4-
import { declareActivitiesHandler } from "./handler.js";
4+
import {
5+
declareActivitiesHandler,
6+
type ActivityError,
7+
type BoxedActivityImplementations,
8+
} from "./handler.js";
59
import type { ContractDefinition } from "@temporal-contract/contract";
610

711
describe("Worker-Boxed Package", () => {
@@ -61,11 +65,11 @@ describe("Worker-Boxed Package", () => {
6165

6266
// Valid input should work
6367
const result = await handler.activities["processPayment"]!({ amount: 100, currency: "USD" });
64-
expect(result.transactionId).toBe("tx-100");
68+
expect((result as { transactionId: string }).transactionId).toBe("tx-100");
6569

6670
// Invalid input should throw
6771
await expect(
68-
handler.activities["processPayment"]!({ amount: "invalid" as any, currency: "USD" }),
72+
handler.activities["processPayment"]!({ amount: "invalid" as unknown, currency: "USD" }),
6973
).rejects.toThrow();
7074
});
7175

@@ -91,15 +95,19 @@ describe("Worker-Boxed Package", () => {
9195
});
9296

9397
const result = await handler.activities["fetchData"]!({ id: "abc" });
94-
expect(result.data).toBe("data-abc");
95-
expect(result.timestamp).toBe(123);
98+
expect((result as { data: string; timestamp: number }).data).toBe("data-abc");
99+
expect((result as { data: string; timestamp: number }).timestamp).toBe(123);
96100

97101
// Invalid output should throw
98102
const badHandler = declareActivitiesHandler({
99103
contract,
100104
activities: {
101-
fetchData: (_args) => {
102-
return Future.value(Result.Ok({ data: "test" } as any)); // Missing timestamp
105+
fetchData: (
106+
_args,
107+
): Future<Result<{ data: string; timestamp: number }, ActivityError>> => {
108+
return Future.value(
109+
Result.Ok({ data: "test" } as unknown as { data: string; timestamp: number }),
110+
); // Missing timestamp
103111
},
104112
},
105113
});
@@ -129,7 +137,7 @@ describe("Worker-Boxed Package", () => {
129137
});
130138

131139
const result = await handler.activities["successActivity"]!({ value: "test" });
132-
expect(result.result).toBe("success-test");
140+
expect((result as { result: string }).result).toBe("success-test");
133141
});
134142

135143
it("should handle Result.Error by throwing exception", async () => {
@@ -162,10 +170,14 @@ describe("Worker-Boxed Package", () => {
162170
try {
163171
await handler.activities["failingActivity"]!({ value: "test" });
164172
expect.fail("Should have thrown an error");
165-
} catch (error: any) {
166-
expect(error.message).toBe("Something went wrong");
167-
expect(error.code).toBe("ACTIVITY_FAILED");
168-
expect(error.details).toEqual({ info: "additional details" });
173+
} catch (error: unknown) {
174+
expect((error as Error & { code: string; details?: unknown }).message).toBe(
175+
"Something went wrong",
176+
);
177+
expect((error as Error & { code: string; details?: unknown }).code).toBe("ACTIVITY_FAILED");
178+
expect((error as Error & { code: string; details?: unknown }).details).toEqual({
179+
info: "additional details",
180+
});
169181
}
170182
});
171183

@@ -185,7 +197,7 @@ describe("Worker-Boxed Package", () => {
185197
contract,
186198
activities: {
187199
asyncActivity: (args) => {
188-
return Future.make<Result<{ completed: boolean }, any>>((resolve) => {
200+
return Future.make<Result<{ completed: boolean }, ActivityError>>((resolve) => {
189201
setTimeout(() => {
190202
resolve(Result.Ok({ completed: true }));
191203
}, args.delay);
@@ -195,7 +207,7 @@ describe("Worker-Boxed Package", () => {
195207
});
196208

197209
const result = await handler.activities["asyncActivity"]!({ delay: 10 });
198-
expect(result.completed).toBe(true);
210+
expect((result as { completed: boolean }).completed).toBe(true);
199211
});
200212

201213
it("should support workflow-specific activities", async () => {
@@ -224,8 +236,8 @@ describe("Worker-Boxed Package", () => {
224236
},
225237
});
226238

227-
const result = await handler.activities["validateOrder"]!({ orderId: "ORDER-123" });
228-
expect(result.valid).toBe(true);
239+
const result = await handler.activities["validateOrder"]!({ orderId: "123", amount: 100 });
240+
expect((result as { valid: boolean }).valid).toBe(true);
229241
});
230242

231243
it("should throw if activity definition is not found", () => {
@@ -239,8 +251,8 @@ describe("Worker-Boxed Package", () => {
239251
declareActivitiesHandler({
240252
contract,
241253
activities: {
242-
unknownActivity: (_args: any) => Future.value(Result.Ok({ result: "test" })),
243-
} as any,
254+
unknownActivity: (_args: unknown) => Future.value(Result.Ok({ result: "test" })),
255+
} as BoxedActivityImplementations<typeof contract>,
244256
});
245257
}).toThrow("Activity definition not found");
246258
});

‎packages/worker-boxed/src/handler.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ export type BoxedActivityImplementations<T extends ContractDefinition> =
118118
/**
119119
* Utility type to convert union to intersection
120120
*/
121-
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
121+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (
122+
k: infer I,
123+
) => void
122124
? I
123125
: never;
124126

@@ -135,7 +137,7 @@ export interface DeclareActivitiesHandlerOptions<T extends ContractDefinition> {
135137
*/
136138
export interface ActivitiesHandler<T extends ContractDefinition> {
137139
contract: T;
138-
activities: Record<string, (...args: any[]) => Promise<any>>;
140+
activities: Record<string, (...args: unknown[]) => Promise<unknown>>;
139141
}
140142

141143
/**
@@ -203,7 +205,7 @@ export function declareActivitiesHandler<T extends ContractDefinition>(
203205
const { contract, activities } = options;
204206

205207
// Wrap activities with validation and Result unwrapping
206-
const wrappedActivities: Record<string, (...args: any[]) => Promise<any>> = {};
208+
const wrappedActivities: Record<string, (...args: unknown[]) => Promise<unknown>> = {};
207209

208210
for (const [activityName, activityImpl] of Object.entries(activities)) {
209211
// Find activity definition (global or workflow-specific)
@@ -226,29 +228,31 @@ export function declareActivitiesHandler<T extends ContractDefinition>(
226228
throw new Error(`Activity definition not found for: ${activityName}`);
227229
}
228230

229-
wrappedActivities[activityName] = async (...args: any[]) => {
231+
wrappedActivities[activityName] = async (...args: unknown[]) => {
230232
// Extract single parameter (Temporal passes as args array)
231233
const input = args.length === 1 ? args[0] : args;
232234

233235
// Validate input
234-
const validatedInput = activityDef.input.parse(input) as any;
236+
const validatedInput = activityDef.input.parse(input);
235237

236238
// Execute boxed activity (pass single parameter, returns Future<Result<T, E>>)
237-
const futureResult = (activityImpl as any)(validatedInput);
239+
const futureResult = (
240+
activityImpl as (args: unknown) => Future<Result<unknown, ActivityError>>
241+
)(validatedInput);
238242

239243
// Unwrap Future and Result
240244
const result = await futureResult.toPromise();
241245

242246
return result.match({
243-
Ok: (value: any) => {
247+
Ok: (value: unknown) => {
244248
// Validate output on success
245249
return activityDef.output.parse(value);
246250
},
247251
Error: (error: ActivityError) => {
248252
// Convert Result.Error to thrown exception for Temporal
249-
const err = new Error(error.message);
250-
(err as any).code = error.code;
251-
(err as any).details = error.details;
253+
const err = new Error(error.message) as Error & { code: string; details?: unknown };
254+
err.code = error.code;
255+
err.details = error.details;
252256
throw err;
253257
},
254258
});

‎packages/worker/src/handler.spec.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ describe("Worker Package", () => {
7676

7777
// Valid input should work - Temporal passes as array
7878
const result = await handler.activities["processPayment"]!({ amount: 100, currency: "USD" });
79-
expect(result.transactionId).toBe("tx-100");
79+
expect((result as { transactionId: string }).transactionId).toBe("tx-100");
8080

8181
// Invalid input should throw
8282
await expect(
83-
handler.activities["processPayment"]!({ amount: "invalid" as any, currency: "USD" }),
83+
handler.activities["processPayment"]!({ amount: "invalid" as unknown, currency: "USD" }),
8484
).rejects.toThrow();
8585
});
8686

@@ -99,8 +99,8 @@ describe("Worker Package", () => {
9999
const handler = declareActivitiesHandler({
100100
contract,
101101
activities: {
102-
fetchData: async (_args) => {
103-
return { data: "test", timestamp: "invalid" } as any;
102+
fetchData: async (_args): Promise<{ data: string; timestamp: number }> => {
103+
return { data: "test", timestamp: "invalid" as unknown as number };
104104
},
105105
},
106106
});
@@ -175,24 +175,40 @@ describe("Worker Package", () => {
175175
});
176176

177177
// Valid input (Temporal passes as array)
178-
const result = await workflow([{ orderId: "123", amount: 100 }] as any);
178+
const result = await workflow([{ orderId: "123", amount: 100 }] as unknown as {
179+
orderId: string;
180+
amount: number;
181+
});
179182
expect(result.status).toBe("completed");
180183
expect(result.total).toBe(100);
181184

182185
// Invalid input should throw
183-
await expect(workflow([{ orderId: 123, amount: "invalid" }] as any)).rejects.toThrow();
186+
await expect(
187+
workflow([{ orderId: 123, amount: "invalid" }] as unknown as {
188+
orderId: string;
189+
amount: number;
190+
}),
191+
).rejects.toThrow();
184192
});
185193

186194
it("should validate workflow output", async () => {
187195
const workflow = declareWorkflow({
188196
definition: testWorkflowDef,
189197
contract: testContract,
190-
implementation: async (_context, _args) => {
191-
return { status: "completed", total: "invalid" } as any;
198+
implementation: async (_context, _args): Promise<{ status: string; total: number }> => {
199+
return { status: "completed", total: "invalid" } as unknown as {
200+
status: string;
201+
total: number;
202+
};
192203
},
193204
});
194205

195-
await expect(workflow([{ orderId: "123", amount: 100 }] as any)).rejects.toThrow();
206+
await expect(
207+
workflow([{ orderId: "123", amount: 100 }] as unknown as {
208+
orderId: string;
209+
amount: number;
210+
}),
211+
).rejects.toThrow();
196212
});
197213

198214
it("should register signal handlers", async () => {
@@ -308,7 +324,10 @@ describe("Worker Package", () => {
308324
});
309325

310326
// Single parameter (should extract from array)
311-
await workflow([{ orderId: "123", amount: 100 }] as any);
327+
await workflow([{ orderId: "123", amount: 100 }] as unknown as {
328+
orderId: string;
329+
amount: number;
330+
});
312331
});
313332
});
314333
});

0 commit comments

Comments
 (0)