Skip to content

Commit 72acdd7

Browse files
authored
feat!: introduce Display trait with extensible type printing (#85)
Replace Print<T> with Display<T> and establish KitTraits global namespace for type-level traits. Changes: - Add KitTraits.Display.Handlers<$Type> extensible interface - Move display.ts to src/utils/ts/traits/ for clear trait organization - Co-locate built-in handlers in domain modules (arr, prom, fn, obj, str, time) - Update imports to use #ts/ts pattern - Rename Print → Display, ShowHandlers → Handlers BREAKING CHANGE: Print<T> renamed to Display<T>, KitLibrarySettings.Print namespace replaced by KitTraits.Display
1 parent 4bc906a commit 72acdd7

File tree

12 files changed

+393
-207
lines changed

12 files changed

+393
-207
lines changed

src/domains/arr/arr.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,24 @@ export const transpose = <$T>(rows: readonly (readonly $T[])[]): $T[][] => {
234234
}
235235

236236
// TODO: Add immutable array operations that wrap ArrMut with copy semantics
237+
238+
/**
239+
* Display handlers for Array types.
240+
* @internal
241+
*/
242+
import type { Display } from '#ts/ts'
243+
declare global {
244+
namespace KitTraits.Display {
245+
// dprint-ignore
246+
interface Handlers<$Type> {
247+
// Array (mutable)
248+
_array: $Type extends (infer __element__)[] ? `Array<${Display<__element__>}>` : never
249+
// ReadonlyArray - only matches if NOT also a mutable array
250+
_readonlyArray: $Type extends readonly (infer __element__)[]
251+
? $Type extends (infer __unused__)[]
252+
? never // It's mutable, let _array handler win
253+
: `ReadonlyArray<${Display<__element__>}>`
254+
: never
255+
}
256+
}
257+
}

src/domains/fn/fn.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ export * as Kind from './kind.js'
99
export * from './partial/__.js'
1010
export * from './pipe.js'
1111
export * from './predicates.js'
12+
13+
/**
14+
* Display handler for Function type.
15+
* @internal
16+
*/
17+
declare global {
18+
namespace KitTraits.Display {
19+
interface Handlers<$Type> {
20+
_function: $Type extends Function ? 'Function' : never
21+
}
22+
}
23+
}

src/domains/obj/obj.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,15 @@ export type ToParametersExact<
344344
export type PropertyKeyToString<$Key extends PropertyKey> = $Key extends string ? $Key
345345
: $Key extends number ? `${$Key}`
346346
: never
347+
348+
/**
349+
* Display handler for symbol type.
350+
* @internal
351+
*/
352+
declare global {
353+
namespace KitTraits.Display {
354+
interface Handlers<$Type> {
355+
_symbol: $Type extends symbol ? 'symbol' : never
356+
}
357+
}
358+
}

src/domains/prom/prom.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,16 @@ export function maybeAsync<T, R = T, E = unknown>(
264264

265265
return envelope.value as any
266266
}
267+
268+
/**
269+
* Display handler for Promise type.
270+
* @internal
271+
*/
272+
import type { Display } from '#ts/ts'
273+
declare global {
274+
namespace KitTraits.Display {
275+
interface Handlers<$Type> {
276+
_promise: $Type extends Promise<infer __value__> ? `Promise<${Display<__value__>}>` : never
277+
}
278+
}
279+
}

src/domains/str/match.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,15 @@ export const isNotMatchAnyOn = Fn.curry(isNotMatchAny)
245245
* @returns Function that takes a value and returns boolean
246246
*/
247247
export const isNotMatchAnyWith = Fn.flipCurried(isNotMatchAnyOn)
248+
249+
/**
250+
* Display handler for RegExp type.
251+
* @internal
252+
*/
253+
declare global {
254+
namespace KitTraits.Display {
255+
interface Handlers<$Type> {
256+
_regExp: $Type extends RegExp ? 'RegExp' : never
257+
}
258+
}
259+
}

src/domains/time/duration.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ export const format = (milliseconds: number): Formatted => {
8383

8484
return { value, unit }
8585
}
86+
87+
/**
88+
* Display handler for Date type.
89+
* @internal
90+
*/
91+
declare global {
92+
namespace KitTraits.Display {
93+
interface Handlers<$Type> {
94+
_date: $Type extends Date ? 'Date' : never
95+
}
96+
}
97+
}

src/utils/ts/__.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './ts.js'
22

3-
export * from './print.js'
3+
export * from './traits/display.js'
44

55
// @ts-expect-error Duplicate identifier
66
export * as SimpleSignature from './simple-signature.js'

src/utils/ts/print.test.ts

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/utils/ts/print.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)