Skip to content

Commit

Permalink
further type hardening and fixing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nonergodic committed Nov 19, 2024
1 parent dd12bbb commit c085b1c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions core/base/src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ import type { RoPair, RoTuple, RoArray, Extends, Xor, Not } from './metaprogramm
export type RoTuple2D<T = unknown> = RoTuple<RoTuple<T>>;
export type RoArray2D<T = unknown> = RoArray<RoArray<T>>;

export type TupleWithLength<T, L extends number, A extends T[] = []> =
A["length"] extends L ? A : TupleWithLength<T, L, [...A, T]>;

export type RoTupleWithLength<T, L extends number> = Readonly<TupleWithLength<T, L>>;

export type TupleRange<L extends number, A extends number[] = []> =
type TupleRangeImpl<L extends number, A extends number[] = []> =
A["length"] extends L
? A
: TupleRange<L, [...A, A["length"]]>;
: TupleRangeImpl<L, [...A, A["length"]]>;

export type Range<L extends number> =
export type TupleRange<L extends number> =
number extends L
? number[]
: TupleRange<L>;
? never
: L extends any
? TupleRangeImpl<L>
: never;

export type Range<L extends number> =
L extends any
? number extends L
? number[]
: TupleRange<L>
: never;

export type TupleWithLength<T, L extends number> =
TupleRange<L> extends infer R extends RoArray<number>
? [...{ [K in keyof R]: T }]
: never;

export type RoTupleWithLength<T, L extends number> = Readonly<TupleWithLength<T, L>>;

export const range = <const L extends number>(length: L) =>
[...Array(length).keys()] as Range<L>;
Expand All @@ -27,7 +38,7 @@ export type IndexEs = number;
//utility type to reduce boilerplate of iteration code by replacing:
// `T extends readonly [infer Head extends T[number], ...infer Tail extends RoTuple<T[number]>]`
//with just:
// `T extends HeadTail<infer Head, infer Tail>`
// `T extends HeadTail<T, infer Head, infer Tail>`
//this also avoids the somewhat common mistake of accidentally dropping the readonly modifier
export type HeadTail<T extends RoTuple, Head extends T[number], Tail extends RoTuple<T[number]>> =
readonly [Head, ...Tail];
Expand Down

0 comments on commit c085b1c

Please sign in to comment.