首页 > 其他分享 >[Typescript] 140. Extreme - Integers Comparator

[Typescript] 140. Extreme - Integers Comparator

时间:2022-12-19 15:46:02浏览次数:52  
标签:Integers Lower Typescript Greater Comparator Comparison extends Expect type

Implement a type-level integers comparator. We've provided an enum for indicating the comparison result, like this:

  • If a is greater than b, type should be Comparison.Greater.
  • If a and b are equal, type should be Comparison.Equal.
  • If a is lower than b, type should be Comparison.Lower.

Note that a and b can be positive integers or negative integers or zero, even one is positive while another one is negative.

enum Comparison {
  Greater,
  Equal,
  Lower,
}
export type IsNegative<T extends number> =
  NumberToString<T> extends `-${number}` ? true : false;
export type NumberToString<T extends number> = `${T}`;
type ToNumber<
  S extends string,
  ACC extends unknown[] = []
> = S extends `${number}`
  ? S extends `${ACC["length"]}`
    ? ACC["length"]
    : ToNumber<S, [...ACC, unknown]>
  : never;
type Absolute<T extends number> = `${T}` extends `-${infer A}` ? ToNumber<A> : T;
type NegativeComparator<T extends number, U extends number, ACC extends unknown[] = []> = T extends U
  ? Comparison.Equal
  : ACC['length'] extends Absolute<T>
  ? Comparison.Greater
  : ACC['length'] extends Absolute<U>
    ? Comparison.Lower
    : NegativeComparator<T, U, [...ACC, unknown]>;
type PositiveComparator<T extends number, U extends number, ACC extends unknown[] = []> = T extends U
  ? Comparison.Equal
  : ACC['length'] extends T
  ? Comparison.Lower
  : ACC['length'] extends U
    ? Comparison.Greater
    : PositiveComparator<T, U, [...ACC, unknown]>;

type Comparator<A extends number, B extends number> = [IsNegative<A>, IsNegative<B>] extends [infer SINGALA, infer SINGALB]
  ? [SINGALA, SINGALB] extends [true, true] 
    ? NegativeComparator<A, B>
    : [SINGALA, SINGALB] extends [false, false] 
      ? PositiveComparator<A, B>
      : [SINGALA, SINGALB] extends [true, false]
        ? Comparison.Lower
        : [SINGALA, SINGALB] extends [false, true]
          ? Comparison.Greater
          : never
  : never;


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

type cases = [
  Expect<Equal<Comparator<5, 5>, Comparison.Equal>>,
  Expect<Equal<Comparator<5, 6>, Comparison.Lower>>,
  Expect<Equal<Comparator<5, 8>, Comparison.Lower>>,
  Expect<Equal<Comparator<5, 0>, Comparison.Greater>>,
  Expect<Equal<Comparator<-5, 0>, Comparison.Lower>>,
  Expect<Equal<Comparator<0, 0>, Comparison.Equal>>,
  Expect<Equal<Comparator<0, -5>, Comparison.Greater>>,
  Expect<Equal<Comparator<5, -3>, Comparison.Greater>>,
  Expect<Equal<Comparator<5, -7>, Comparison.Greater>>,
  Expect<Equal<Comparator<-5, -7>, Comparison.Greater>>,
  Expect<Equal<Comparator<-5, -3>, Comparison.Lower>>,
  Expect<Equal<Comparator<-25, -30>, Comparison.Greater>>,
  Expect<Equal<Comparator<15, -23>, Comparison.Greater>>,
  Expect<Equal<Comparator<40, 37>, Comparison.Greater>>,
  Expect<Equal<Comparator<-36, 36>, Comparison.Lower>>,
  Expect<Equal<Comparator<27, 27>, Comparison.Equal>>,
  Expect<Equal<Comparator<-38, -38>, Comparison.Equal>>,
]

 

标签:Integers,Lower,Typescript,Greater,Comparator,Comparison,extends,Expect,type
From: https://www.cnblogs.com/Answer1215/p/16992313.html

相关文章

  • Typescript类型体操 - Combination
    题目中文给定一个字符串数组,实现它的全排列组合.EnglishGivenanarrayofstrings,doPermutation&Combination.It'salsousefulfortheproptypeslikevid......
  • Comparator选择排序器
    步骤1让元素所属的类实现Comparator接口2重写compare​(To1,To2)方法比较两个参数的顺序。3o1,o2是接收Compareator的实现类对象规则:o1-o2——>升序o2-o1——......
  • 源码解读之TypeScript类型覆盖检测工具type-coverage
    因为团队内部开启了一个持续的前端代码质量改进计划,其中一个专项就是TS类型覆盖率,期间用到了type-coverage这个仓库,所以借这篇文章分享一下这个工具,并顺便从源码阅读的角......
  • [Typescript] 139. Extreme - Slice
    ImplementtheJavaScript Array.slice functioninthetypesystem. Slice<Arr,Start,End> takesthethreeargument.Theoutputshouldbeasubarrayof Arr......
  • TypeScript & styled-components props type error All In One
    TypeScript&styled-componentspropstypeerrorAllInOneenvinfohttps://www.npmjs.com/package/envinfo$npxenvinfo--system--binaries--npmPackagessty......
  • TypeScript dynamic object key type All In One
    TypeScriptdynamicobjectkeytypeAllInOneinterfaceObj{//......
  • [Typescript] @typescript-eslint/unbound-method
    Itisusefultoenable '@typescript-eslint/unbound-method':'error',becausethiskindoferrorisrelatedtothiskeyword,sometimeitishardtonoticedur......
  • Typescript TSConfig
     "references":[  {"path":"modules/xxxx"},  {"path":"modules/xxx"}, ], reference可以提高构建速度 "compilerOptions":{  "paths......
  • [Typescript] 138. Hard - SnakeCase
    Createa SnakeCase<T> genericthatturnsastringformattedin camelCase intoastringformattedin snake_case.Afewexamples:typeres1=SnakeCase<"he......
  • Comparator与Comparable用法与区别
    一、概述。  Comparator和Comparable两者都属于集合框架的一部分,都是用来在对象之间进行比较的,但两者又有些许的不同,我们先通过一个例子来看一下他们的区别,然后再分别学习......