Skip to content

Instantly share code, notes, and snippets.

@Lunchb0ne
Created December 24, 2023 11:46
Show Gist options
  • Save Lunchb0ne/9045e23b82e5509b6fdddbfb20333654 to your computer and use it in GitHub Desktop.
Save Lunchb0ne/9045e23b82e5509b6fdddbfb20333654 to your computer and use it in GitHub Desktop.
Advent of Typescript Day 13
// There's only so many ways you can do an accumulator in ts
type Accumulator<N extends number, Acc extends ReadonlyArray<unknown> = []> = Acc extends {
length: N;
}
? Acc
: Accumulator<N, [...Acc, unknown]>;
// Make a Union of the indexes excluding 0
type IndexUnion<A extends ReadonlyArray<unknown>> = Exclude<
{
[K in keyof A]: K extends `${infer N extends number}` ? N : never;
}[number],
0
>;
// Can be used to together to hack together the range needed
type DayCounter<S extends number, E extends number> =
| Exclude<IndexUnion<Accumulator<E>>, IndexUnion<Accumulator<S>>>
| E;
type x = IndexUnion<Accumulator<5>>;
// ^?
type y = IndexUnion<Accumulator<10>>;
// ^?
type days = DayCounter<5, 10>;
// ^?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment