CheckRepeatedchars
介绍
判断一个string类型中是否有相同的字符
ts
type CheckRepeatedChars<T extends string> = any
View on GitHubts
type CheckRepeatedChars<T extends string> = any
起点
ts
/* _____________ Your Code Here _____________ */typeCheckRepeatedChars <T extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'abc'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'abb'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'cbc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <''>, false>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeCheckRepeatedChars <T extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'abc'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'abb'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <'cbc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <CheckRepeatedChars <''>, false>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeCheckRepeatedChars <T extends string> =T extends `${inferF }${inferE }`?E extends `${string}${F }${string}`? true:CheckRepeatedChars <E >: false
ts
// most populartypeCheckRepeatedChars <T extends string> =T extends `${inferF }${inferE }`?E extends `${string}${F }${string}`? true:CheckRepeatedChars <E >: false
ts
// mytypestrToUnion <T extends string> =T extends `${inferF }${inferR }` ?F |strToUnion <R > : nevertypeCheckRepeatedChars <T extends string> =T extends `${inferF }${inferR }` ?F extendsstrToUnion <R > ? true :CheckRepeatedChars <R > : false
ts
// mytypestrToUnion <T extends string> =T extends `${inferF }${inferR }` ?F |strToUnion <R > : nevertypeCheckRepeatedChars <T extends string> =T extends `${inferF }${inferR }` ?F extendsstrToUnion <R > ? true :CheckRepeatedChars <R > : false