首页 > 其他分享 >[Typescript] 63. Medium - Greater Than

[Typescript] 63. Medium - Greater Than

时间:2022-10-23 02:11:07浏览次数:57  
标签:Typescript false true 63 GreaterThan Expect extends type Than

In This Challenge, You should implement a type GreaterThan<T, U> like T > U

Negative numbers do not need to be considered.

For example

GreaterThan<2, 1> //should be true
GreaterThan<1, 1> //should be false
GreaterThan<10, 100> //should be false
GreaterThan<111, 11> //should be true

 

/* _____________ Your Code Here _____________ */

export type MinusOne<T extends number, ARR extends unknown[] = []> = any extends never ? never: [...ARR, 1]['length'] extends T ? ARR['length'] : MinusOne<T, [...ARR, 1]>;
type GreaterThan<T extends number, U extends number> = 
  T extends 0 ? false : MinusOne<T> extends U ? true: GreaterThan<MinusOne<T>, U>;


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<GreaterThan<1, 0>, true>>,
  Expect<Equal<GreaterThan<5, 4>, true>>,
  Expect<Equal<GreaterThan<4, 5>, false>>,
  Expect<Equal<GreaterThan<0, 0>, false>>,
  Expect<Equal<GreaterThan<20, 20>, false>>,
  Expect<Equal<GreaterThan<10, 100>, false>>,
  Expect<Equal<GreaterThan<111, 11>, true>>,
]

 

type GreaterThan<T extends number, U extends number, ACC extends unknown[] = []> = 
  T extends ACC['length']
    ? false
    : U extends ACC['length']
      ? true
      : GreaterThan<T, U, [...ACC, unknown]>;

 

标签:Typescript,false,true,63,GreaterThan,Expect,extends,type,Than
From: https://www.cnblogs.com/Answer1215/p/16817775.html

相关文章