Skip to content

Instantly share code, notes, and snippets.

@mscolnick
Created March 15, 2021 04:08
Show Gist options
  • Save mscolnick/71ff1400ec02de4696ee6afb51fa5d20 to your computer and use it in GitHub Desktop.
Save mscolnick/71ff1400ec02de4696ee6afb51fa5d20 to your computer and use it in GitHub Desktop.
type _Fib<NUM extends any[]> = NUM['length'] extends 1 | 0
? NUM
: Add<_Fib<Minus1<NUM>>, _Fib<Minus2<NUM>>>;
type Fib<N extends number> = _Fib<ArrayOfSize<N>>['length'];
type F1 = Fib<1>; // 1
type F2 = Fib<2>; // 1
type F3 = Fib<3>; // 2
type F4 = Fib<4>; // 3
type F5 = Fib<5>; // 5
type F6 = Fib<6>; // 8
type F7 = Fib<7>; // 13
type F8 = Fib<8>; // 21
type F9 = Fib<9>; // 34
type F20 = Fib<20>; // 6765
type F30 = Fib<30>; // Error, recursion too deep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment