Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「重学TS 2.0 」TS 练习题第三十四题 #53

Open
semlinker opened this issue Sep 22, 2021 · 8 comments
Open

「重学TS 2.0 」TS 练习题第三十四题 #53

semlinker opened this issue Sep 22, 2021 · 8 comments

Comments

@semlinker
Copy link
Owner

实现一个 SmallerThan 工具类型,用于比较数值类型的大小。具体的使用示例如下所示:

type SmallerThan<
  N extends number,
  M extends number,
> = // 你的实现代码

type S0 = SmallerThan<0, 1>; // true
type S1 = SmallerThan<2, 0>; // false
type S2 = SmallerThan<8, 10>; // true

请在下面评论你的答案

@xiaoYuanDun
Copy link

xiaoYuanDun commented Sep 23, 2021

TS中没有直接对比数值的操作,所以需要用 extends 来代替,达到类似的目的,这里默认从一个空数组开始递增,每次判断这个数组的长度和 M,N 是否相同

  • 辅助判断的数组 S 是从长度为 0 开始递增的,所以第一个和 L 相等的数一定是两个数中较小的那一个(有可能相等)
  • 若 N 与 L 相等,则 N <= M,还需要一次额外判断
  •    若 M 与 L 也相等,则 N === M
  •    若 M 不等于 L,则 N < M
  • 若 N 与 L 不相等,则继续判断 M
  •    若 M 等于 L,这 N > M
  •    若 M 也不等于 L,则表示 M,N 都不匹配,增加 S 长度,继续做下一次 SmallerThan 判断
type SmallerThan<N extends number, M extends number, S extends any[] = [], L = S['length']> = 
  L extends N 
    ? L extends M ? false : true 
    : L extends M ? false : SmallerThan<N, M, [...S, 1]>

//  测试用例
type S0 = SmallerThan<0, 1>; // true
type S1 = SmallerThan<2, 0>; // false
type S2 = SmallerThan<8, 10>; // true
type S3 = SmallerThan<5, 5>; // false

@zhaoxiongfei
Copy link

type SmallerThan<
  N extends number,
  M extends number,
  A extends any[] = []
> = A["length"] extends M
  ? false
  : A["length"] extends N
    ? true
    : SmallerThan<N, M, [...A, ""]>

type S0 = SmallerThan<0, 1>; // true
type S1 = SmallerThan<2, 0>; // false
type S2 = SmallerThan<8, 10>; // true
type S3 = SmallerThan<8, 8>; // false

思路: 依然是利用构造数组的长度来判断,体用递归逐步迭代,先和哪个数匹配上,哪个数就小,注意边界问题。
这里要求的是第一个数小,如果相等,返回自然是false

@xq52301nzdm
Copy link

type SmallerThan<
  N extends number,
  M extends number,
  A extends any[] = []
> = A["length"] extends M
  ? false
  : A["length"] extends N
    ? true
    : SmallerThan<N, M, [...A, ""]>

type S0 = SmallerThan<0, 1>; // true
type S1 = SmallerThan<2, 0>; // false
type S2 = SmallerThan<8, 10>; // true
type S3 = SmallerThan<8, 8>; // false

思路: 依然是利用构造数组的长度来判断,体用递归逐步迭代,先和哪个数匹配上,哪个数就小,注意边界问题。 这里要求的是第一个数小,如果相等,返回自然是false
神操,6,大佬学到了!

@ChuTingzj
Copy link

// 实现一个 SmallerThan 工具类型,用于比较数值类型的大小。具体的使用示例如下所示:
// type SmallerThan<
//   N extends number,
//   M extends number,
// > = // 你的实现代码
// type S0 = SmallerThan<0, 1>; // true
// type S1 = SmallerThan<2, 0>; // false
// type S2 = SmallerThan<8, 10>; // true

//answer
type SmallerThan<N extends number, M extends number, A extends any[] = []> = N extends A['length'] ? true : M extends A['length'] ? false : SmallerThan<N, M, [...A, 1]>
type S00 = SmallerThan<0, 1> // true
type S11 = SmallerThan<2, 0> // false
type S22 = SmallerThan<8, 10> // true

@xq52301nzdm
Copy link

xq52301nzdm commented Jun 12, 2022 via email

@zhengyimeng
Copy link

type SmallerThan<
  N extends number,
  M extends number,
  A extends any[] = []
> = N extends A['length']
  ? M extends A['length']
    ? false
    : true
  : M extends A['length']
    ? false
    : SmallerThan<N, M, [...A, '']>

type S0 = SmallerThan<0, 1>; // true
type S1 = SmallerThan<2, 0>; // false
type S2 = SmallerThan<8, 10>; // true

@silencefore
Copy link

type SmallerThan<N extends number,
M extends number,
> = keyof Repeat<1, N> extends keyof Repeat<1, M>?true:false;// 你的实现代码

type S0111 = SmallerThan<0, 1>; // true

type S122 = SmallerThan<2, 0>; // false
type S233 = SmallerThan<8, 10>; // true

@xq52301nzdm
Copy link

xq52301nzdm commented Apr 3, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants