Implement type `CheckRepeatedChars<T>` which will return whether type `T` contains duplicated member Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement CheckRepeatedTuple<T> which checks whether a tuple type contains any duplicated members, returning true or false.
Implement type CheckRepeatedChars<T> which will return whether type T contains duplicated member
For example:
type CheckRepeatedTuple<[1, 2, 3]> // false
type CheckRepeatedTuple<[1, 2, 1]> // trueChange the following code to make the test cases pass (no type check errors).
type cases = [
Expect<Equal<CheckRepeatedTuple<[number, number, string, boolean]>, true>>,
Expect<Equal<CheckRepeatedTuple<[number, string]>, false>>,
Expect<Equal<CheckRepeatedTuple<[1, 2, 3]>, false>>,
Expect<Equal<CheckRepeatedTuple<[1, 2, 1]>, true>>,
Expect<Equal<CheckRepeatedTuple<[]>, false>>,
Expect<Equal<CheckRepeatedTuple<string[]>, false>>,
Expect<
Equal<
CheckRepeatedTuple<
[number, 1, string, '1', boolean, true, false, unknown, any]
>,
false
>
>,
Expect<Equal<CheckRepeatedTuple<[never, any, never]>, true>>,
]
Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.
One-time payment. Lifetime access.
type Includes<T extends unknown[], V> = T extends [infer First, ...infer Rest]
? Equal<First, V> extends true
? true
: Includes<Rest, V>
: false
type CheckRepeatedTuple<T extends unknown[]> = T extends [infer First, ...infer Rest]
? Includes<Rest, First> extends true
? true
: CheckRepeatedTuple<Rest>
: falseHow it works:
Includes<T, V> is a helper type that checks if a value V exists anywhere in the tuple T using strict Equal comparisons to handle edge cases like any, never, and unknownCheckRepeatedTuple<T> processes the tuple by extracting the first element with infer First and the rest with ...infer RestIncludes<Rest, First>trueCheckRepeatedTuple<Rest> to check subsequent elementsfalse, meaning no duplicates were foundEqual instead of extends ensures correct handling of special types -- for example, any and unknown are treated as distinct typesThis challenge helps you understand recursive tuple analysis with strict type equality, and how to apply these concepts in real-world scenarios.
This challenge is originally from here.