Type Challenges Judge

Fibonacci Sequence

提出詳細

type NumToArr<T extends number, Arr extends readonly unknown[]=[]> = T extends Arr['length'] ? Arr : NumToArr<T,[...Arr, 0]> type Pred<T extends number> = NumToArr<T> extends [infer Head, ...infer Rest] ? Rest["length"] : -1 // using recursion error suppression technique // https://kgtkr.net/blog/2019/04/15/typescript-typelevelprogramming-error-suppression type Sum<T extends number, U extends number> = NumToArr<T> extends infer TArr extends readonly unknown[] ? NumToArr<U> extends infer UArr extends readonly unknown[] ? [...TArr, ...UArr]["length"] : never : never type Fibonacci<T extends number> = T extends 1 | 2 ? 1 : Fibonacci<Pred<Pred<T>>> extends infer N extends number ? Fibonacci<Pred<T>> extends infer M extends number ? Sum<N,M> : never : never
提出日時2023-09-18 02:41:42
問題Fibonacci Sequence
ユーザーsankantsu
ステータスAccepted
テストケース
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Fibonacci<3>, 2>>, Expect<Equal<Fibonacci<8>, 21>>, ]