跳到主要内容

Fibonacci

介绍

实现斐波那契序列. 接收数值T, 返回它相关的斐波那契数列(https://en.wikipedia.org/wiki/Fibonacci_number).

序列: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

ts
type Fibonacci<T extends number> = any
type Result1 = Fibonacci<3> // 2
type Result2 = Fibonacci<8> // 21
ts
type Fibonacci<T extends number> = any
type Result1 = Fibonacci<3> // 2
type Result2 = Fibonacci<8> // 21
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Fibonacci<T extends number> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Fibonacci<1>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<2>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<3>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<8>, 21>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
ts
/* _____________ Your Code Here _____________ */
 
type Fibonacci<T extends number> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Fibonacci<1>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<2>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<3>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Fibonacci<8>, 21>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
// most popular
 
type Fibonacci<T extends number> = any
 
ts
// most popular
 
type Fibonacci<T extends number> = any
 
view more solutions