代码拉取完成,页面将自动刷新
/*
847 - String Join
-------
by Matt Davis (@tl-matt-davis) #hard
### Question
Create a type-safe string join utility which can be used like so:
```ts
const hyphenJoiner = join('-')
const result = hyphenJoiner('a', 'b', 'c'); // = 'a-b-c'
```
Or alternatively:
```ts
join('#')('a', 'b', 'c') // = 'a#b#c'
```
When we pass an empty delimiter (i.e '') to join, we should concat the strings as they are, i.e:
```ts
join('')('a', 'b', 'c') // = 'abc'
```
When only one item is passed, we should get back the original item (without any delimiter added):
```ts
join('-')('a') // = 'a'
```
> View on GitHub: https://tsch.js.org/847
*/
/* _____________ Your Code Here _____________ */
type Test<T extends string[], key extends string, str extends string = T[0]> = T extends [infer R extends string, ...infer F extends string[]] ? Test<F, key, `${str}${key}${R}`> : str
type Test1 = Test<['1', '2'], ','>
declare function join<T extends string[], K extends string>(delimiter: K): (...parts: T) => T extends [infer F extends string, ...infer R] ? 1 : 2
/* _____________ Test Cases _____________ */
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'>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/847/answer
> View solutions: https://tsch.js.org/847/solutions
> More Challenges: https://tsch.js.org
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。