Typescript 크거나 같거나 작음

유형 수준 숫자 비교기

export type CreateArrayWithLengthX<
    LENGTH extends number,
    ACC extends unknown[] = []
> = ACC['length'] extends LENGTH
    ? ACC
    : CreateArrayWithLengthX<LENGTH, [...ACC, 1]>

export type CompareAToB<
    A extends number[],
    B extends number[]
> = A['length'] extends B['length']
    ? 'equal'
    : A extends [infer C, ...infer D extends number[]]
    ? B extends [infer M, ...infer N extends number[]]
        ? CompareAToB<D, N>
        : 'greater'
    : B['length'] extends 0
    ? 'equal'
    : 'lesser'

type E = CompareAToB<CreateArrayWithLengthX<99>,CreateArrayWithLengthX<100>> // lesser
//   ^?
type F = CompareAToB<CreateArrayWithLengthX<100>,CreateArrayWithLengthX<100>> // equal 
//   ^?
type G = CompareAToB<CreateArrayWithLengthX<101>,CreateArrayWithLengthX<100>> // greater
//   ^?


playground

최대 유효 숫자는 1000입니다.

좋은 웹페이지 즐겨찾기