rimo030 / type-challenges

Collection of TypeScript type challenges with online judge
https://tsch.js.org/
MIT License
3 stars 0 forks source link

no - 4425 Greater Than #57

Closed rimo030 closed 7 months ago

rimo030 commented 7 months ago
type NArray<T extends number, R extends any[] = []> = R['length'] extends T ? R : NArray<T, [...R, 0]>

type GreaterThan<T extends number, U extends number> = [...NArray<T>][U] extends undefined ? false : true
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<10, 9>, true>>,
  Expect<Equal<GreaterThan<20, 20>, false>>,
  Expect<Equal<GreaterThan<10, 100>, false>>,
  Expect<Equal<GreaterThan<111, 11>, true>>,
  Expect<Equal<GreaterThan<1234567891011, 1234567891010>, true>>,
]
rimo030 commented 7 months ago

배열을 생성하여 비교했었다. 재귀 한계를 넘어 에러가 발생한다.

다른 방법을 생각해보쟈! 🤔

type NArray<T extends number, R extends any[] = []> = R['length'] extends T ? R : NArray<T, [...R, 0]>

type GreaterThan<T extends number, U extends number> = [...NArray<T>][U] extends undefined ? false : true
rimo030 commented 7 months ago

https://github.com/rimo030/type-challenges/issues/76 으로 이어집니다!