Skip to content

Instantly share code, notes, and snippets.

@yohamta
Created November 18, 2021 03:52
Show Gist options
  • Save yohamta/37e8b34fe2195cb4d30e50c3dbbaf88e to your computer and use it in GitHub Desktop.
Save yohamta/37e8b34fe2195cb4d30e50c3dbbaf88e to your computer and use it in GitHub Desktop.
Shitty add function in typescript
type Len<T extends any[]> = T["length"]
type _Add<
T,
N1 extends Number,
N2 extends Number,
Curr1 extends any[],
Curr2 extends any[]
> = Len<Curr1> extends N1
? Len<Curr2> extends N2
? Len<[...Curr1, ...Curr2]>
: _Add<T, N1, N2, Curr1, [T, ...Curr2]>
: _Add<T, N1, N2, [T, ...Curr1], Curr2>;
type Add<N1 extends Number, N2 extends Number> = _Add<any, N1, N2, [], []>
type X = Add<3, 5>; // type X = 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment