다음과 같이 타입-세이프한 문자열을 join하는 유틸함수를 만들어라:
const hyphenJoiner = join('-')
const result = hyphenJoiner('a', 'b', 'c'); // = 'a-b-c'
혹은 대신해서:
join('#')('a', 'b', 'c') // = 'a#b#c'
만약 빈 구분자(예: '')로 join 한다면, 우리는 다음과 같이 문자열을 합쳐야 한다.
예시:
join('')('a', 'b', 'c') // = 'abc'
한 요소만 넘겨질 때, 우리는 (어떠한 구분자를 추가하지 않고) 원본 요소만 되돌려줘야한다.
join('-')('a') // = 'a'
Create a type-safe string join utility which can be used like so:
const hyphenJoiner = join('-')
const result = hyphenJoiner('a', 'b', 'c'); // = 'a-b-c'
Or alternatively:
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:
join('')('a', 'b', 'c') // = 'abc'
When only one item is passed, we should get back the original item (without any delimiter added):
join('-')('a') // = 'a'
type JoinResult<Arr extends string[],D extends string>=
Arr extends [infer S extends string,...infer R extends string[]]?
R['length'] extends 0?S:
`${S}${D}${JoinResult<R,D>}`
:''
declare function join<D extends string>(delimiter: D): <Arr extends string[]>(...parts: Arr) => JoinResult<Arr,D>
Join을 실제 함수에 적용하는 문제이다. 제네릭을 통해 적용할 수 있다