[Typescript] 73. Medium - Join

Implement the type version of Array.join, Join<T, U> takes an Array T, string or number U and returns the Array T with U stitching up.

type Res = Join<["a", "p", "p", "l", "e"], "-">; // expected to be 'a-p-p-l-e'
type Res1 = Join<["Hello", "World"], " ">; // expected to be 'Hello World'
type Res2 = Join<["2", "2", "2"], 1>; // expected to be '21212'
type Res3 = Join<["o"], "u">; // expected to be 'o'

 

/* _____________ Your Code Here _____________ */

type Join<T extends string[], U extends string | number, ACC extends string = ''> = T extends [infer A extends string, ...infer RT extends string[]]
  ? RT['length'] extends 0
    ? `${ACC}${A}`
    : Join<RT, U ,`${ACC}${A}${U}`>
  : ACC;

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Join<['a', 'p', 'p', 'l', 'e'], '-'>, 'a-p-p-l-e'>>,
  Expect<Equal<Join<['Hello', 'World'], ' '>, 'Hello World'>>,
  Expect<Equal<Join<['2', '2', '2'], 1>, '21212'>>,
  Expect<Equal<Join<['o'], 'u'>, 'o'>>,
]

 

posted @   Zhentiw  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-28 [Kotlin] When
2020-10-28 [Kotlin] The Let function
2020-10-28 [Kotlin] The Elvis operator
2017-10-28 [ReasonML] Workshops code
2017-10-28 [ReasonML] Named & optional params
2017-10-28 [REASONML] Using Javascript npm package from REASON
2016-10-28 [Ionic2] Device Interaction in an Ionic App with Cordova Plugins
点击右上角即可分享
微信分享提示