rivo
Version:
🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.
47 lines (40 loc) • 1.5 kB
TypeScript
import type { Ary, AryL } from ".";
import type { Args, Fn } from "../HKT";
import type { List } from "../List";
import type { Show, TypeClass$$Show } from "../typeclass";
import type { Show as Show_ } from "../typeclass/Show/Show";
declare module "../typeclass/Show" {
interface ShowImpl {
array: ImplShowFor<AryL<ShowL>, Ary$$Show>;
}
}
/**
* Implementation of the {@link Show} type class for {@link Ary} (i.e., `Array` and `ReadonlyArray`).
*/
export interface Ary$$Show extends TypeClass$$Show<Ary<Show>> {
Show: Ary$$Show$ShowFn;
}
/**
* [Fn] Show an {@link Ary} (i.e., `Array` or `ReadonlyArray`).
*/
interface Ary$$Show$ShowFn extends Fn<[Ary<Show>], string> {
// @ts-ignore - Return type annotation circularly references itself.
def: ([xs]: Args<this>) => Ary$$Show$Show<typeof xs>;
}
/**
* Show an {@link Ary} (i.e., `Array` or `ReadonlyArray`).
*/
type Ary$$Show$Show<TS extends Ary<Show>> =
[TS] extends [unknown[]] ?
number extends TS["length"] ?
// @ts-ignore - Type instantiation is excessively deep and possibly infinite.
`Array<${Show_<TS[number]>}>`
: `[${ShowTuple<TS>}]`
: number extends TS["length"] ? `ReadonlyArray<${Show_<TS[number]>}>`
: `readonly [${ShowTuple<TS>}]`;
type ShowW<S> = [S] extends [Show] ? Show_<S> : never;
type ShowTuple<TS extends List<Show>> =
[TS] extends [readonly [infer T]] ? ShowW<T>
: [TS] extends [readonly [infer H, ...infer T extends List<Show>]] ?
`${ShowW<H>}, ${ShowTuple<T>}`
: "";