type-challenges:CheckRepeatedChars

32 阅读1分钟

CheckRepeatedChars

问题描述

判断一个string类型中是否有相同的字符

type CheckRepeatedChars<'abc'>   // false
type CheckRepeatedChars<'aba'>   // true
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
import { ExpectFalse, NotEqual } from './test-utils'type cases = [
  Expect<Equal<CheckRepeatedChars<'abc'>, false>>,
  Expect<Equal<CheckRepeatedChars<'abb'>, true>>,
  Expect<Equal<CheckRepeatedChars<'cbc'>, true>>,
  Expect<Equal<CheckRepeatedChars<''>, false>>,
]
​
​
// ============= Your Code Here =============
// 答案
  type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer R}` 
  ? R extends `${string}${F}${string}`
    ? true
    : CheckRepeatedChars<R>
  : false