튜플에서 처음 나온 나온 S를 R로 바꾸어 반환하는 ReplaceFirst<T, S, R>를 구현하라.
만약 S가 T에 없다면, 결과는 T여야 한다.
Implement the type ReplaceFirst<T, S, R> which will replace the first occurrence of S in a tuple T with R. If no such S exists in T, the result should be T.
type ReplaceFirst<T extends readonly unknown[], S, R> =
T extends [infer First, ...infer Rest]?
First extends S?
[R,...Rest]
:[First,...ReplaceFirst<Rest,S,R>]
:[]
재귀적으로 문제를 해결했다.
T의 첫번째 요소가 S를 확장가능하면 R로 바꾸어 나머지 요소와 함께 반환한다.
그렇지 않다면 첫 번째 요소를 그대로 넣고, 나머지 요소들을 가지고 재귀를 실행한다.
종료조건은 T가 빈 배열일 경우이다.