String Join
提出詳細
type JoinerImpl<T extends readonly unknown[], D extends string> = T extends [infer T1 extends string, ...infer T2] ? T2 extends [] ? T1 : `${T1}${D}${JoinerImpl<T2,D>}` : "" type Joiner<D extends string> = <T extends string[]>(...args: T) => JoinerImpl<T,D> declare function join<D extends string>(delimiter: D): Joiner<D>
提出日時 | 2023-09-20 01:45:58 |
---|---|
問題 | String Join |
ユーザー | sankantsu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' // Edge cases const noCharsOutput = join('-')() const oneCharOutput = join('-')('a') const noDelimiterOutput = join('')('a', 'b', 'c') // Regular cases const hyphenOutput = join('-')('a', 'b', 'c') const hashOutput = join('#')('a', 'b', 'c') const twoCharOutput = join('-')('a', 'b') const longOutput = join('-')('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') type cases = [ Expect<Equal<typeof noCharsOutput, ''>>, Expect<Equal<typeof oneCharOutput, 'a'>>, Expect<Equal<typeof noDelimiterOutput, 'abc'>>, Expect<Equal<typeof twoCharOutput, 'a-b'>>, Expect<Equal<typeof hyphenOutput, 'a-b-c'>>, Expect<Equal<typeof hashOutput, 'a#b#c'>>, Expect<Equal<typeof longOutput, 'a-b-c-d-e-f-g-h'>>, ]