ts 实现fib

原创
10/02 16:43
阅读数 0

类型推导和计算

namespace fib {
  type Length<T extends any[]> = T["length"];
  type Range<T extends Number = 0, P extends any[] = []> = {
    0: Range<T, [any, ...P]>;
    1: P;
  }[Length<P> extends T ? 1 : 0];

  type Concat<T extends any[], P extends any[]> = [...T, ...P];
  type t1 = Range<3>;
  // type t1 = [any, any, any]

  type Zero = Length<Range<0>>;
  //   type Zero = 0
  type Ten = Length<Range<10>>;
  // type Ten = 10

  type Five = Length<Range<5>>;
  // type Five = 5

  type One = Length<Range<1>>;
  type Add<T extends number, P extends number> = Length<
    Concat<Range<T>, Range<P>>
  >;

  type Two = Add<One, One>;
  //   type Two = 2

  type Tail<T extends any[]> = ((...t: T) => any) extends (
    _: any,
    ...tail: infer P
  ) => any
    ? P
    : [];

  type Append<T extends any[], E = any> = [...T, E];
  type IsEmpty<T extends any[]> = Length<T> extends 0 ? true : false;
  type NotEmpty<T extends any[]> = IsEmpty<T> extends true ? false : true;
  type t4 = IsEmpty<Range<0>>;
  // type t4 = true

  type t5 = IsEmpty<Range<1>>;
  // type t5 = false

  type And<T extends boolean, P extends boolean> = T extends false
    ? false
    : P extends false
    ? false
    : true;
  type t6 = And<true, true>;
  // type t6 = true

  type t7 = And<true, false>;
  // type t7 = false

  type t8 = And<false, false>;
  // type t8 = false

  type t9 = And<false, true>;
  // type t9 = false

  //   T <= P
  type LessList<T extends any[], P extends any[]> = {
    0: LessList<Tail<T>, Tail<P>>;
    1: true;
    2: false;
  }[And<NotEmpty<T>, NotEmpty<P>> extends true
    ? 0
    : IsEmpty<T> extends true
    ? 1
    : 2];
  type Less<T extends number, P extends number> = LessList<Range<T>, Range<P>>;

  type t10 = Less<Zero, One>;
  // type t10 = true
  type t11 = Less<One, Zero>;
  // type t11 = false

  type t12 = Less<One, One>;
  // type t12 = true

  // T - P
  type SubList<T extends any[], P extends any[], R extends any[] = []> = {
    0: Length<R>;
    1: SubList<Tail<T>, P, Append<R>>;
  }[Length<T> extends Length<P> ? 0 : 1];
  type t13 = SubList<Range<10>, Range<5>>;
  // type t13 = 5

  // 由于集合大小不能为负数
  type Sub<T extends number, P extends number> = {
    0: Sub<P, T>;
    1: SubList<Range<T>, Range<P>>;
  }[Less<T, P> extends true ? 0 : 1];

  type t14 = Sub<One, Zero>;
  //   type t14 = 1
  type t15 = Sub<Ten, Five>;
  // type t15 = 5

  type Fib<T extends number> = {
    0: T;
    1: Add<Fib<Sub<T, One>>, Fib<Sub<T, Two>>>;
  }[Less<T, One> extends true ? 0 : 1];

  type r0 = Fib<Zero>;
  // type r10= 0
  type r1 = Fib<One>;
  // type r1 = 1

  type r2 = Fib<Two>;
  // type r2 = 1

  type r3 = Fib<3>;
  // type r3 = 2

  type r4 = Fib<4>;
  // type r4 = 3

  type r5 = Fib<5>;
  //type r5 = 5

  type r6 = Fib<6>;
  //   type r6 = 8
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部