Skip to content

Commit 348b7a0

Browse files
committed
Replace any types with meaningful types across library utilities
Replace any with unknown or proper exported types (RetryOptions, IterationOptions) in core library files for improved type safety and developer experience. Files updated: - env.ts: Replace any with unknown in conversion functions - parse-args.ts: Replace any with unknown in coerce functions - json.ts: Replace any with unknown in JsonReviver and utility functions - objects.ts: Replace any with unknown in object manipulation functions - debug.ts: Replace any with proper types (unknown, string, InspectOptions) - promises.ts: Add RetryOptions and IterationOptions interfaces, replace any - streams.ts: Add proper types for streaming-iterables, use IterationOptions - prompts.ts: Replace any with unknown, add proper Context typing - spinner.ts: Replace any with unknown in method parameters - logger.ts: Replace any with unknown in Logger class methods
1 parent 8c58cf4 commit 348b7a0

File tree

16 files changed

+755
-182
lines changed

16 files changed

+755
-182
lines changed

registry/src/lib/debug.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ function customLog() {
101101
debugJs.inspectOpts.showHidden === null
102102
? undefined
103103
: debugJs.inspectOpts.showHidden,
104+
depth:
105+
debugJs.inspectOpts.depth === null ||
106+
typeof debugJs.inspectOpts.depth === 'boolean'
107+
? undefined
108+
: debugJs.inspectOpts.depth,
104109
}
105110
: {}
106111
ReflectApply(logger.info, logger, [
@@ -113,7 +118,7 @@ function customLog() {
113118
* @private
114119
*/
115120
/*@__NO_SIDE_EFFECTS__*/
116-
function extractOptions(namespaces: any) {
121+
function extractOptions(namespaces: NamespacesOrOptions) {
117122
return namespaces !== null && typeof namespaces === 'object'
118123
? { __proto__: null, ...namespaces }
119124
: { __proto__: null, namespaces }
@@ -124,7 +129,7 @@ function extractOptions(namespaces: any) {
124129
* @private
125130
*/
126131
/*@__NO_SIDE_EFFECTS__*/
127-
function isEnabled(namespaces: any) {
132+
function isEnabled(namespaces: string | undefined) {
128133
if (typeof namespaces !== 'string' || !namespaces || namespaces === '*') {
129134
return true
130135
}
@@ -154,16 +159,29 @@ function isEnabled(namespaces: any) {
154159
* Debug output for object inspection.
155160
*/
156161
/*@__NO_SIDE_EFFECTS__*/
157-
/* c8 ignore start - Debug utilities only used in development. */
158-
function debugDirNs(namespacesOrOpts: any, obj: any, inspectOpts?: any) {
162+
function debugDirNs(
163+
namespacesOrOpts: NamespacesOrOptions,
164+
obj: unknown,
165+
inspectOpts?: InspectOptions | undefined,
166+
) {
159167
const options = extractOptions(namespacesOrOpts)
160168
const { namespaces } = options
161169
if (!isEnabled(namespaces)) {
162170
return
163171
}
164172
if (inspectOpts === undefined) {
165173
const debugJs = getDebugJs()
166-
inspectOpts = debugJs.inspectOpts
174+
const opts = debugJs.inspectOpts
175+
if (opts) {
176+
inspectOpts = {
177+
...opts,
178+
showHidden: opts.showHidden === null ? undefined : opts.showHidden,
179+
depth:
180+
opts.depth === null || typeof opts.depth === 'boolean'
181+
? null
182+
: opts.depth,
183+
} as InspectOptions
184+
}
167185
}
168186
const { spinner = /*@__PURE__*/ require('./constants/spinner.js') } = options
169187
const wasSpinning = spinner.isSpinning
@@ -174,15 +192,13 @@ function debugDirNs(namespacesOrOpts: any, obj: any, inspectOpts?: any) {
174192
spinner.start()
175193
}
176194
}
177-
/* c8 ignore stop */
178195

179196
let pointingTriangle: string | undefined
180197
/**
181198
* Debug output with function name prefix.
182199
*/
183200
/*@__NO_SIDE_EFFECTS__*/
184-
/* c8 ignore start - Debug utilities only used in development. */
185-
function debugFnNs(namespacesOrOpts: NamespacesOrOptions, ...args: any[]) {
201+
function debugFnNs(namespacesOrOpts: NamespacesOrOptions, ...args: unknown[]) {
186202
const options = extractOptions(namespacesOrOpts)
187203
const { namespaces } = options
188204
if (!isEnabled(namespaces)) {
@@ -248,14 +264,12 @@ function debugFnNs(namespacesOrOpts: NamespacesOrOptions, ...args: any[]) {
248264
spinner.start()
249265
}
250266
}
251-
/* c8 ignore stop */
252267

253268
/**
254269
* Debug logging function.
255270
*/
256271
/*@__NO_SIDE_EFFECTS__*/
257-
/* c8 ignore start - Debug utilities only used in development. */
258-
function debugLogNs(namespacesOrOpts: any, ...args: any[]) {
272+
function debugLogNs(namespacesOrOpts: NamespacesOrOptions, ...args: unknown[]) {
259273
const options = extractOptions(namespacesOrOpts)
260274
const { namespaces } = options
261275
if (!isEnabled(namespaces)) {
@@ -269,62 +283,55 @@ function debugLogNs(namespacesOrOpts: any, ...args: any[]) {
269283
spinner.start()
270284
}
271285
}
272-
/* c8 ignore stop */
273286

274287
/**
275288
* Check if debug mode is enabled.
276289
*/
277290
/*@__NO_SIDE_EFFECTS__*/
278-
/* c8 ignore start - Debug utilities only used in development. */
279-
function isDebugNs(namespaces: any): boolean {
291+
function isDebugNs(namespaces: string | undefined): boolean {
280292
return ENV.SOCKET_CLI_DEBUG && isEnabled(namespaces)
281293
}
282-
/* c8 ignore stop */
283294

284295
/**
285296
* Simple debug check based on DEBUG environment variable.
286297
*/
287298
/*@__NO_SIDE_EFFECTS__*/
288-
/* c8 ignore start - Debug utilities only used in development. */
289299
export function isDebugSimple(): boolean {
290300
const debug = process.env['DEBUG']
291301
if (!debug || debug === '' || debug === '0' || debug === 'false') {
292302
return false
293303
}
294304
return true
295305
}
296-
/* c8 ignore stop */
297306

298307
/**
299308
* Simple debug log that logs to console when DEBUG is set.
300309
*/
301310
/*@__NO_SIDE_EFFECTS__*/
302-
/* c8 ignore start - Debug utilities only used in development. */
303-
export function debugLogSimple(...args: any[]): void {
311+
export function debugLogSimple(...args: unknown[]): void {
304312
if (isDebugSimple()) {
305313
console.log(...args)
306314
}
307315
}
308-
/* c8 ignore stop */
309316

310317
/**
311318
* Simple debug dir that logs object to console when DEBUG is set.
312319
*/
313320
/*@__NO_SIDE_EFFECTS__*/
314-
/* c8 ignore start - Debug utilities only used in development. */
315-
export function debugDirSimple(obj: any, options?: any): void {
321+
export function debugDirSimple(
322+
obj: unknown,
323+
options?: InspectOptions | undefined,
324+
): void {
316325
if (isDebugSimple()) {
317326
console.dir(obj, options || { depth: null, colors: true })
318327
}
319328
}
320-
/* c8 ignore stop */
321329

322330
/**
323331
* Simple debug function that creates a namespaced debug logger.
324332
*/
325333
/*@__NO_SIDE_EFFECTS__*/
326-
/* c8 ignore start - Debug utilities only used in development. */
327-
function matchPattern(ns: any, pattern: any) {
334+
function matchPattern(ns: string, pattern: string) {
328335
if (pattern === '*') {
329336
return true
330337
}
@@ -342,8 +349,8 @@ function matchPattern(ns: any, pattern: any) {
342349
/**
343350
* Create a simple debug function without external dependencies.
344351
*/
345-
export function debugFnSimple(namespace: any) {
346-
const log = (...args: any[]) => {
352+
export function debugFnSimple(namespace: string) {
353+
const log = (...args: unknown[]) => {
347354
const debug = process.env['DEBUG'] || ''
348355

349356
// Parse debug patterns.
@@ -379,45 +386,41 @@ export function debugFnSimple(namespace: any) {
379386

380387
return log
381388
}
382-
/* c8 ignore stop */
383389

384390
/**
385391
* Create a debug logger similar to util.debuglog.
386392
*/
387393
/*@__NO_SIDE_EFFECTS__*/
388-
/* c8 ignore start - Debug utilities only used in development. */
389-
export function debuglog(section: any) {
390-
const log = (...args: any[]) => {
394+
export function debuglog(section: string) {
395+
const log = (...args: unknown[]) => {
391396
if (isDebugSimple()) {
392397
console.log(`[${section}]`, ...args)
393398
}
394399
}
395400
return log
396401
}
397-
/* c8 ignore stop */
398402

399403
/**
400404
* Create a debug timer.
401405
*/
402406
/*@__NO_SIDE_EFFECTS__*/
403-
/* c8 ignore start - Debug utilities only used in development. */
404-
export function debugtime(section: any) {
407+
export function debugtime(section: string) {
405408
const timers = new Map()
406409

407-
const timer = (label: any) => {
410+
const timer = (label: string) => {
408411
if (isDebugSimple()) {
409412
console.log(`[${section}] ${label}`)
410413
}
411414
}
412415

413-
timer.start = (label: any) => {
416+
timer.start = (label: string) => {
414417
if (isDebugSimple()) {
415418
timers.set(label, Date.now())
416419
console.log(`[${section}] ${label}: start`)
417420
}
418421
}
419422

420-
timer.end = (label: any) => {
423+
timer.end = (label: string) => {
421424
if (isDebugSimple()) {
422425
const start = timers.get(label)
423426
if (start) {
@@ -430,7 +433,6 @@ export function debugtime(section: any) {
430433

431434
return timer
432435
}
433-
/* c8 ignore stop */
434436

435437
// Export aliases for compatibility.
436438
export { debugDirSimple as debugDir }

registry/src/lib/env.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const StringCtor = String
1616
* Convert an environment variable value to a boolean.
1717
*/
1818
/*@__NO_SIDE_EFFECTS__*/
19-
export function envAsBoolean(value: any, defaultValue = false): boolean {
19+
export function envAsBoolean(value: unknown, defaultValue = false): boolean {
2020
if (typeof value === 'string') {
2121
const trimmed = value.trim()
2222
return trimmed === '1' || trimmed.toLowerCase() === 'true'
@@ -31,8 +31,8 @@ export function envAsBoolean(value: any, defaultValue = false): boolean {
3131
* Convert an environment variable value to a number.
3232
*/
3333
/*@__NO_SIDE_EFFECTS__*/
34-
export function envAsNumber(value: any, defaultValue = 0): number {
35-
const numOrNaN = NumberParseInt(value, 10)
34+
export function envAsNumber(value: unknown, defaultValue = 0): number {
35+
const numOrNaN = NumberParseInt(String(value), 10)
3636
const numMayBeNegZero = NumberIsFinite(numOrNaN)
3737
? numOrNaN
3838
: NumberCtor(defaultValue)
@@ -44,7 +44,7 @@ export function envAsNumber(value: any, defaultValue = 0): number {
4444
* Convert an environment variable value to a trimmed string.
4545
*/
4646
/*@__NO_SIDE_EFFECTS__*/
47-
export function envAsString(value: any, defaultValue = ''): string {
47+
export function envAsString(value: unknown, defaultValue = ''): string {
4848
if (typeof value === 'string') {
4949
return value.trim()
5050
}

registry/src/lib/json.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface JsonObject {
1515

1616
export interface JsonArray extends Array<JsonValue> {}
1717

18-
export type JsonReviver = (key: string, value: any) => any
18+
export type JsonReviver = (key: string, value: unknown) => unknown
1919

2020
export interface JsonParseOptions {
2121
filepath?: string
@@ -34,26 +34,35 @@ const JSONParse = JSON.parse
3434
*/
3535
/*@__NO_SIDE_EFFECTS__*/
3636
function isBuffer(x: unknown): x is Buffer {
37-
const obj = x as any
38-
if (!obj || typeof obj !== 'object' || typeof obj.length !== 'number') {
37+
if (!x || typeof x !== 'object') {
3938
return false
4039
}
41-
if (typeof obj.copy !== 'function' || typeof obj.slice !== 'function') {
40+
const obj = x as Record<string | number, unknown>
41+
if (typeof obj['length'] !== 'number') {
4242
return false
4343
}
44-
if (obj.length > 0 && typeof obj[0] !== 'number') {
44+
if (typeof obj['copy'] !== 'function' || typeof obj['slice'] !== 'function') {
45+
return false
46+
}
47+
if (
48+
typeof obj['length'] === 'number' &&
49+
obj['length'] > 0 &&
50+
typeof obj[0] !== 'number'
51+
) {
4552
return false
4653
}
4754

48-
const Ctor = obj.constructor as any
49-
return !!(typeof Ctor?.isBuffer === 'function' && Ctor.isBuffer(obj))
55+
const Ctor = (x as { constructor?: unknown }).constructor as
56+
| { isBuffer?: unknown }
57+
| undefined
58+
return !!(typeof Ctor?.isBuffer === 'function' && Ctor.isBuffer(x))
5059
}
5160

5261
/**
5362
* Check if a value is a JSON primitive (null, boolean, number, or string).
5463
*/
5564
/*@__NO_SIDE_EFFECTS__*/
56-
export function isJsonPrimitive(value: any): value is JsonPrimitive {
65+
export function isJsonPrimitive(value: unknown): value is JsonPrimitive {
5766
return (
5867
value === null ||
5968
typeof value === 'boolean' ||

0 commit comments

Comments
 (0)