Skip to content

Commit db6be24

Browse files
Copilothotlong
andcommitted
refactor: reduce any type usage in @objectql/types from ~86 to 2 justified instances
Replace `any` with type-safe alternatives across all type definition files: - Use `Record<string, unknown>` for data objects (create/update payloads) - Use `object` for parameters accepting interfaces (queries, filters, configs) - Use `unknown` for opaque values (transaction handles, return values) - Use generics with `Record<string, unknown>` defaults for hook/action contexts - Make IObjectRepository generic with `<T = Record<string, unknown>>` - Type MetadataRegistry storage with proper Record types Remaining `any` (2 instances in api.ts captureStackTrace) is justified for V8 API compatibility. All downstream builds maintain same error count (pre-existing only). All 1723 tests continue to pass. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 5ae9ba8 commit db6be24

15 files changed

Lines changed: 124 additions & 126 deletions

File tree

packages/foundation/types/src/action.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type ActionInputDefinition = Record<string, FieldConfig>;
4343
*
4444
* RUNTIME TYPE: Used during action execution.
4545
*/
46-
export interface ActionContext<_BaseT = any, InputT = any> {
46+
export interface ActionContext<_BaseT = Record<string, unknown>, InputT = Record<string, unknown>> {
4747
/** The object this action belongs to. */
4848
objectName: string;
4949

@@ -71,7 +71,7 @@ export interface ActionContext<_BaseT = any, InputT = any> {
7171
*/
7272
user?: {
7373
id: string | number;
74-
[key: string]: any;
74+
[key: string]: unknown;
7575
};
7676
}
7777

@@ -122,11 +122,11 @@ export interface ActionConfig {
122122
*
123123
* RUNTIME TYPE: Includes the handler function for execution.
124124
*/
125-
export interface ActionDefinition<BaseT = any, InputT = any, ReturnT = any> extends ActionConfig {
125+
export interface ActionDefinition<BaseT = Record<string, unknown>, InputT = Record<string, unknown>, ReturnT = unknown> extends ActionConfig {
126126
/**
127127
* The business logic implementation.
128128
*/
129129
handler: (ctx: ActionContext<BaseT, InputT>) => Promise<ReturnT>;
130130
}
131131

132-
export type ActionHandler<BaseT = any, InputT = any, ReturnT = any> = (ctx: ActionContext<BaseT, InputT>) => Promise<ReturnT>;
132+
export type ActionHandler<BaseT = Record<string, unknown>, InputT = Record<string, unknown>, ReturnT = unknown> = (ctx: ActionContext<BaseT, InputT>) => Promise<ReturnT>;

packages/foundation/types/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export interface IObjectQL {
2727
triggerHook(event: HookName, objectName: string, ctx: HookContext): Promise<void>;
2828

2929
registerAction(objectName: string, actionName: string, handler: ActionHandler): void;
30-
executeAction(objectName: string, actionName: string, ctx: ActionContext): Promise<any>;
30+
executeAction(objectName: string, actionName: string, ctx: ActionContext): Promise<unknown>;
3131

3232
/**
3333
* Get the underlying ObjectKernel instance
3434
* @returns The ObjectKernel instance
3535
*/
36-
getKernel(): any; // Using 'any' to avoid circular dependency with @objectstack/runtime
36+
getKernel(): unknown; // Using 'unknown' to avoid circular dependency with @objectstack/runtime
3737
}

packages/foundation/types/src/application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ export interface AppConfig {
5050
/**
5151
* Custom metadata/settings
5252
*/
53-
[key: string]: any;
53+
[key: string]: unknown;
5454
}

packages/foundation/types/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ObjectQLContext {
3131
* Execute a function within a transaction.
3232
* The callback receives a new context 'trxCtx' which inherits userId, spaceId from this context.
3333
*/
34-
transaction(callback: (trxCtx: ObjectQLContext) => Promise<any>): Promise<any>;
34+
transaction(callback: (trxCtx: ObjectQLContext) => Promise<unknown>): Promise<unknown>;
3535

3636
/**
3737
* Returns a new context with system privileges (isSystem: true).
@@ -42,7 +42,7 @@ export interface ObjectQLContext {
4242
/**
4343
* Internal: Driver-specific transaction handle.
4444
*/
45-
transactionHandle?: any;
45+
transactionHandle?: unknown;
4646
}
4747

4848
export interface ObjectQLContextOptions {

packages/foundation/types/src/driver.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface IntrospectedColumn {
1717
/** Whether the column is nullable */
1818
nullable: boolean;
1919
/** Default value if any */
20-
defaultValue?: any;
20+
defaultValue?: unknown;
2121
/** Whether this is a primary key */
2222
isPrimary?: boolean;
2323
/** Whether this column has a unique constraint */
@@ -178,41 +178,41 @@ export interface Driver {
178178
readonly supports?: DriverCapabilities;
179179

180180
// Core CRUD methods
181-
find(objectName: string, query: any, options?: any): Promise<any[]>;
182-
findOne(objectName: string, id: string | number, query?: any, options?: any): Promise<any>;
183-
create(objectName: string, data: any, options?: any): Promise<any>;
184-
update(objectName: string, id: string | number, data: any, options?: any): Promise<any>;
185-
delete(objectName: string, id: string | number, options?: any): Promise<any>;
186-
count(objectName: string, filters: any, options?: any): Promise<number>;
181+
find(objectName: string, query: object, options?: Record<string, unknown>): Promise<Record<string, unknown>[]>;
182+
findOne(objectName: string, id: string | number, query?: object, options?: Record<string, unknown>): Promise<Record<string, unknown> | null>;
183+
create(objectName: string, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
184+
update(objectName: string, id: string | number, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
185+
delete(objectName: string, id: string | number, options?: Record<string, unknown>): Promise<unknown>;
186+
count(objectName: string, filters: object, options?: Record<string, unknown>): Promise<number>;
187187

188188
// Lifecycle methods
189189
connect?(): Promise<void>;
190190
disconnect?(): Promise<void>;
191191
checkHealth?(): Promise<boolean>;
192192

193193
// Bulk operations
194-
execute?(command: any, parameters?: any[], options?: any): Promise<any>;
195-
bulkCreate?(objectName: string, data: any[], options?: any): Promise<any>;
196-
bulkUpdate?(objectName: string, updates: Array<{id: string | number, data: any}>, options?: any): Promise<any>;
197-
bulkDelete?(objectName: string, ids: Array<string | number>, options?: any): Promise<any>;
194+
execute?(command: object, parameters?: unknown[], options?: Record<string, unknown>): Promise<unknown>;
195+
bulkCreate?(objectName: string, data: Record<string, unknown>[], options?: Record<string, unknown>): Promise<unknown>;
196+
bulkUpdate?(objectName: string, updates: Array<{id: string | number, data: Record<string, unknown>}>, options?: Record<string, unknown>): Promise<unknown>;
197+
bulkDelete?(objectName: string, ids: Array<string | number>, options?: Record<string, unknown>): Promise<unknown>;
198198

199199
// Query extensions
200-
distinct?(objectName: string, field: string, filters?: any, options?: any): Promise<any[]>;
201-
aggregate?(objectName: string, query: any, options?: any): Promise<any[]>;
200+
distinct?(objectName: string, field: string, filters?: object, options?: Record<string, unknown>): Promise<unknown[]>;
201+
aggregate?(objectName: string, query: object, options?: Record<string, unknown>): Promise<Record<string, unknown>[]>;
202202

203203
// Transaction support
204-
beginTransaction?(): Promise<any>;
204+
beginTransaction?(): Promise<unknown>;
205205
/** @deprecated Use `commit` — aligned with @objectstack/spec DriverInterfaceSchema. Will be removed in v5.0. */
206-
commitTransaction?(transaction: any): Promise<void>;
206+
commitTransaction?(transaction: unknown): Promise<void>;
207207
/** @deprecated Use `rollback` — aligned with @objectstack/spec DriverInterfaceSchema. Will be removed in v5.0. */
208-
rollbackTransaction?(transaction: any): Promise<void>;
208+
rollbackTransaction?(transaction: unknown): Promise<void>;
209209
/** Commit a transaction (spec-aligned name) */
210-
commit?(transaction: any): Promise<void>;
210+
commit?(transaction: unknown): Promise<void>;
211211
/** Rollback a transaction (spec-aligned name) */
212-
rollback?(transaction: any): Promise<void>;
212+
rollback?(transaction: unknown): Promise<void>;
213213

214214
// Schema / Lifecycle
215-
init?(objects: any[]): Promise<void>;
215+
init?(objects: object[]): Promise<void>;
216216

217217
/**
218218
* Introspect the database schema to discover existing tables, columns, and relationships.
@@ -221,32 +221,32 @@ export interface Driver {
221221
introspectSchema?(): Promise<IntrospectedSchema>;
222222

223223
// Bulk / Atomic (alternative signatures)
224-
createMany?(objectName: string, data: any[], options?: any): Promise<any>;
225-
updateMany?(objectName: string, filters: any, data: any, options?: any): Promise<any>;
226-
deleteMany?(objectName: string, filters: any, options?: any): Promise<any>;
227-
findOneAndUpdate?(objectName: string, filters: any, update: any, options?: any): Promise<any>;
224+
createMany?(objectName: string, data: Record<string, unknown>[], options?: Record<string, unknown>): Promise<unknown>;
225+
updateMany?(objectName: string, filters: object, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;
226+
deleteMany?(objectName: string, filters: object, options?: Record<string, unknown>): Promise<unknown>;
227+
findOneAndUpdate?(objectName: string, filters: object, update: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown> | null>;
228228

229229
// DriverInterface v4.0 methods
230230
/**
231231
* Execute a query using QueryAST format (DriverInterface v4.0)
232232
*/
233-
executeQuery?(ast: any, options?: any): Promise<{ value: any[]; count?: number }>;
233+
executeQuery?(ast: object, options?: Record<string, unknown>): Promise<{ value: Record<string, unknown>[]; count?: number }>;
234234

235235
/**
236236
* Execute a command using Command format (DriverInterface v4.0)
237237
*/
238-
executeCommand?(command: any, options?: any): Promise<{ success: boolean; data?: any; affected: number }>;
238+
executeCommand?(command: object, options?: Record<string, unknown>): Promise<{ success: boolean; data?: unknown; affected: number }>;
239239

240240
/**
241241
* Alternative method name for findOne (some drivers use 'get')
242242
*/
243-
get?(objectName: string, id: string, options?: any): Promise<any>;
243+
get?(objectName: string, id: string, options?: Record<string, unknown>): Promise<Record<string, unknown> | null>;
244244

245245
/**
246246
* Direct query execution (legacy)
247247
*/
248-
directQuery?(sql: string, params?: any[]): Promise<any[]>;
249-
query?(sql: string, params?: any[]): Promise<any[]>;
248+
directQuery?(sql: string, params?: unknown[]): Promise<Record<string, unknown>[]>;
249+
query?(sql: string, params?: unknown[]): Promise<Record<string, unknown>[]>;
250250

251251
// ========================================================================
252252
// Methods from @objectstack/spec DriverInterfaceSchema
@@ -261,7 +261,7 @@ export interface Driver {
261261
* @param options - Driver options.
262262
* @returns The upserted record.
263263
*/
264-
upsert?(objectName: string, data: Record<string, unknown>, options?: any): Promise<any>;
264+
upsert?(objectName: string, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
265265

266266
/**
267267
* Stream records matching the structured query.
@@ -272,7 +272,7 @@ export interface Driver {
272272
* @param options - Driver options.
273273
* @returns AsyncIterable/ReadableStream of records.
274274
*/
275-
findStream?(objectName: string, query: any, options?: any): AsyncIterable<any> | any;
275+
findStream?(objectName: string, query: object, options?: Record<string, unknown>): AsyncIterable<Record<string, unknown>> | unknown;
276276

277277
/**
278278
* Get connection pool statistics.
@@ -289,15 +289,15 @@ export interface Driver {
289289
* @param objects - Object definitions to synchronize.
290290
* @param options - Driver options.
291291
*/
292-
syncSchema?(objects: any[], options?: any): Promise<void>;
292+
syncSchema?(objects: object[], options?: Record<string, unknown>): Promise<void>;
293293

294294
/**
295295
* Drop a table/collection by name.
296296
*
297297
* @param objectName - The name of the object/table to drop.
298298
* @param options - Driver options.
299299
*/
300-
dropTable?(objectName: string, options?: any): Promise<void>;
300+
dropTable?(objectName: string, options?: Record<string, unknown>): Promise<void>;
301301

302302
/**
303303
* Explain the execution plan for a query.
@@ -308,6 +308,6 @@ export interface Driver {
308308
* @param options - Driver options.
309309
* @returns Execution plan details.
310310
*/
311-
explain?(objectName: string, query: any, options?: any): Promise<any>;
311+
explain?(objectName: string, query: object, options?: Record<string, unknown>): Promise<unknown>;
312312
}
313313

packages/foundation/types/src/gateway.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export interface ApiRequest {
1616
path: string;
1717
method: ApiMethod;
1818
headers: Record<string, string | string[] | undefined>;
19-
query: Record<string, any>;
20-
body?: any;
19+
query: Record<string, unknown>;
20+
body?: unknown;
2121
protocol?: string; // e.g. 'rest', 'graphql'
2222
}
2323

2424
export interface ApiResponse {
2525
status: number;
2626
headers?: Record<string, string | string[]>;
27-
body: any;
27+
body: unknown;
2828
}
2929

3030
export interface GatewayProtocol {

packages/foundation/types/src/hook.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ export type HookTiming = 'before' | 'after';
2121
* Minimal API surface exposed to hooks for performing side-effects or checks.
2222
*/
2323
export interface HookAPI {
24-
// We use 'any' here to avoid circular dependencies with core/ObjectQL
25-
// In practice, this is the ObjectQL instance.
26-
find(objectName: string, query?: any): Promise<any[]>;
27-
findOne(objectName: string, id: string | number): Promise<any>;
28-
count(objectName: string, query?: any): Promise<number>;
29-
create(objectName: string, data: any): Promise<any>;
30-
update(objectName: string, id: string | number, data: any): Promise<any>;
31-
delete(objectName: string, id: string | number): Promise<any>;
24+
find(objectName: string, query?: Record<string, unknown>): Promise<Record<string, unknown>[]>;
25+
findOne(objectName: string, id: string | number): Promise<Record<string, unknown> | null>;
26+
count(objectName: string, query?: Record<string, unknown>): Promise<number>;
27+
create(objectName: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
28+
update(objectName: string, id: string | number, data: Record<string, unknown>): Promise<Record<string, unknown>>;
29+
delete(objectName: string, id: string | number): Promise<Record<string, unknown>>;
3230
}
3331

3432
/**
3533
* Base context available in all hooks.
3634
*/
37-
export interface BaseHookContext<_T = any> {
35+
export interface BaseHookContext<_T = Record<string, unknown>> {
3836
/** The name of the object (entity) being acted upon. */
3937
objectName: string;
4038

@@ -47,24 +45,24 @@ export interface BaseHookContext<_T = any> {
4745
/** User/Session context (Authentication info). */
4846
user?: {
4947
id: string | number;
50-
[key: string]: any;
48+
[key: string]: unknown;
5149
};
5250

5351
/**
5452
* Shared state for passing data between matching 'before' and 'after' hooks.
5553
* e.g. Calculate a diff in 'beforeUpdate' and read it in 'afterUpdate'.
5654
*/
57-
state: Record<string, any>;
55+
state: Record<string, unknown>;
5856
}
5957

6058
/**
6159
* Context for Retrieval operations (Find, Count).
6260
*/
63-
export interface RetrievalHookContext<T = any> extends BaseHookContext<T> {
61+
export interface RetrievalHookContext<T = Record<string, unknown>> extends BaseHookContext<T> {
6462
operation: 'find' | 'count';
6563

6664
/** The query criteria being executed. Modifiable in 'before' hooks. */
67-
query: any;
65+
query: object;
6866

6967
/** The result of the query. Only available in 'after' hooks. */
7068
result?: T[] | number;
@@ -73,7 +71,7 @@ export interface RetrievalHookContext<T = any> extends BaseHookContext<T> {
7371
/**
7472
* Context for Modification operations (Create, Update, Delete).
7573
*/
76-
export interface MutationHookContext<T = any> extends BaseHookContext<T> {
74+
export interface MutationHookContext<T = Record<string, unknown>> extends BaseHookContext<T> {
7775
operation: 'create' | 'update' | 'delete';
7876

7977
/** The record ID. Undefined for 'create'. */
@@ -103,7 +101,7 @@ export interface MutationHookContext<T = any> extends BaseHookContext<T> {
103101
/**
104102
* Specialized context for Updates, including change tracking.
105103
*/
106-
export interface UpdateHookContext<T = any> extends MutationHookContext<T> {
104+
export interface UpdateHookContext<T = Record<string, unknown>> extends MutationHookContext<T> {
107105
operation: 'update';
108106

109107
/**
@@ -116,7 +114,7 @@ export interface UpdateHookContext<T = any> extends MutationHookContext<T> {
116114
/**
117115
* Definition interface for a set of hooks for a specific object.
118116
*/
119-
export interface ObjectHookDefinition<T = any> {
117+
export interface ObjectHookDefinition<T = Record<string, unknown>> {
120118
beforeFind?: (ctx: RetrievalHookContext<T>) => Promise<void> | void;
121119
afterFind?: (ctx: RetrievalHookContext<T>) => Promise<void> | void;
122120

@@ -134,5 +132,5 @@ export interface ObjectHookDefinition<T = any> {
134132
}
135133

136134
export type HookName = keyof ObjectHookDefinition;
137-
export type HookContext<T = any> = RetrievalHookContext<T> | MutationHookContext<T> | UpdateHookContext<T>;
138-
export type HookHandler<T = any> = (ctx: HookContext<T>) => Promise<void> | void;
135+
export type HookContext<T = Record<string, unknown>> = RetrievalHookContext<T> | MutationHookContext<T> | UpdateHookContext<T>;
136+
export type HookHandler<T = Record<string, unknown>> = (ctx: HookContext<T>) => Promise<void> | void;

packages/foundation/types/src/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ export interface LoaderPlugin {
2121
name: string;
2222
glob: string[];
2323
handler: LoaderHandler;
24-
options?: any;
24+
options?: Record<string, unknown>;
2525
}

packages/foundation/types/src/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,5 @@ export interface ObjectDoc {
196196
_id?: string | number;
197197
created_at?: Date | string;
198198
updated_at?: Date | string;
199-
[key: string]: any;
199+
[key: string]: unknown;
200200
}

packages/foundation/types/src/permission.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface SimpleCondition {
101101
/** Comparison operator */
102102
operator: PermissionOperator;
103103
/** Value to compare against (can include variables like $current_user.id) */
104-
value: any;
104+
value: unknown;
105105
}
106106

107107
/**
@@ -110,7 +110,7 @@ export interface SimpleCondition {
110110
export interface ConditionElement {
111111
field: string;
112112
operator: PermissionOperator;
113-
value: any;
113+
value: unknown;
114114
}
115115

116116
/**
@@ -266,7 +266,7 @@ export interface ActionCondition {
266266
/** Comparison operator */
267267
operator: PermissionOperator;
268268
/** Value to compare */
269-
value: any;
269+
value: unknown;
270270
}
271271

272272
/**
@@ -458,7 +458,7 @@ export interface PermissionCheckContext {
458458
roles?: string[];
459459
department_id?: string;
460460
team_id?: string;
461-
[key: string]: any;
461+
[key: string]: unknown;
462462
};
463463
/** Object name */
464464
object: string;
@@ -469,7 +469,7 @@ export interface PermissionCheckContext {
469469
/** Field name (for field-level checks) */
470470
field?: string;
471471
/** Record data (for condition evaluation) */
472-
record?: any;
472+
record?: Record<string, unknown>;
473473
}
474474

475475
/**

0 commit comments

Comments
 (0)