자바스크립트 런타임 없이 타입스크립트로 해결해보자


type Cons<A, B extends unknown[]> = [A, ...B];
type Car<A extends unknown[]> = A[0];
type Cdr<A extends unknown[]> = A extends [unknown, ...infer R] ? R : never;

type Concat<A extends unknown[], B extends unknown[]> = A extends []
? B
: Cons<Car<A>, Concat<Cdr<A>, B>>;

type Encode<N extends number, R extends unknown[] = []> = R["length"] extends N
? R
: Encode<N, Cons<unknown, R>>;
type Decode<A extends unknown[]> = A["length"];

type _Pad<N extends unknown[], S extends string> = N extends []
? S
: _Pad<Cdr<N>, ` ${S} `>;

type Pad<N extends number, S extends string> = _Pad<Encode<N>, S>;
type Dup<S extends string> = `${S} ${S}`;

type MapDup<S extends unknown[]> = S extends []
? []
: S extends [string, ...string[]]
? Cons<Dup<Car<S>>, MapDup<Cdr<S>>>
: never;

type MapPad<N extends number, S extends unknown[]> = N extends 0
? S
: S extends []
? []
: S extends [string, ...string[]]
? Cons<Pad<N, Car<S>>, MapPad<N, Cdr<S>>>
: never;

type Next<S extends string[]> = Concat<MapPad<Decode<S>, S>, MapDup<S>>;

type Print<S extends unknown[]> = S extends []
? ""
: S extends [string, ...string[]]
? `${Car<S>}\n${Print<Cdr<S>>}`
: never;

type Format<S extends string> = `\n${S}`;

type PrettyPrint<S extends string[]> = Format<Print<S>>;

type L0 = [" * ", " * * ", "*****"];
type L1 = Next<L0>;
type L2 = Next<L1>;




type Sol = PrettyPrint<L2>;


런타임이 없어서



뿌슝코드 타입 복사해서 크롬에 붙여넣음


근데 타입 추론 뎁스에 한계가 있어서 더 못그림;