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

27958 - CheckRepeatedTuple #28009

Open
jiaowoxiaobala opened this issue Jun 7, 2023 · 0 comments
Open

27958 - CheckRepeatedTuple #28009

jiaowoxiaobala opened this issue Jun 7, 2023 · 0 comments
Labels
27958 answer Share answers/solutions to a question zh-CN 简体中文

Comments

@jiaowoxiaobala
Copy link
Contributor

type IsEqual<A, B> = (<X>() => X extends A ? 1 : 2) extends <X>() => X extends B
  ? 1
  : 2
  ? true
  : false;

type Includes<T extends unknown[], U> = T extends [infer F, ...infer Rest]
  ? IsEqual<F, U> extends true
    ? true
    : Includes<Rest, U>
  : false;

// number extends T['length']排除非元组类型,因为T有可能是string[]
// type arr1 = string[] -> arr1['length'] -> number
// type arr2 = [string] -> arr2['length'] -> 1
type CheckRepeatedTuple<T extends unknown[]> = number extends T["length"]
  ? false
  : // 递归遍历T,每次取出第一项
  T extends [infer F, ...infer Rest]
  ? // 判断后面的所有成员中是否有与第一项相等的
    Includes<Rest, F> extends true
    ? true
    : // 如果没有,继续递归后面的所有成员,此时第一项可以排除了
      CheckRepeatedTuple<Rest>
  : false;
@jiaowoxiaobala jiaowoxiaobala added answer Share answers/solutions to a question zh-CN 简体中文 labels Jun 7, 2023
@github-actions github-actions bot added the 27958 label Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
27958 answer Share answers/solutions to a question zh-CN 简体中文
Projects
None yet
Development

No branches or pull requests

1 participant