#9142Medium

CheckRepeatedChars

Implement type CheckRepeatedChars<S> which will return whether type S contains duplicated chars. Learn union type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement CheckRepeatedChars<S> which checks whether a string type contains any duplicated characters, returning true or false.

Challenge Instructions: CheckRepeatedChars

Medium

Implement type CheckRepeatedChars<S> which will return whether type S contains duplicated chars?

For example:

type CheckRepeatedChars<'abc'>   // false
type CheckRepeatedChars<'aba'>   // true

Change the following code to make the test cases pass (no type check errors).

ChallengeSolution
type cases = [
  Expect<Equal<CheckRepeatedChars<'abc'>, false>>,
  Expect<Equal<CheckRepeatedChars<'abb'>, true>>,
  Expect<Equal<CheckRepeatedChars<'cbc'>, true>>,
  Expect<Equal<CheckRepeatedChars<''>, false>>,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

Detailed Explanation

type CheckRepeatedChars<T extends string> =
  T extends `${infer First}${infer Rest}`
    ? Rest extends `${string}${First}${string}`
      ? true
      : CheckRepeatedChars<Rest>
    : false

How it works:

This challenge helps you understand template literal type pattern matching and recursive string analysis, and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts