Lodash.without의 타입버전을 구현하라
Without은 배열 T를 받고 숫자나 배열 U를 받는다.
그리고 U의 요소가 없는 배열을 반환한다.
Implement the type version of Lodash.without, Without<T, U> takes an Array T, number or array U and returns an Array without the elements of U.
type WithoutImplement<T extends any,U,Target = U extends any[]?U[number]:U>=
T extends [infer Start,...infer Rest]?
Start extends Target?
WithoutImplement<Rest,U,Target>
:[Start,...WithoutImplement<Rest,U,Target>]
:[]
type Without<T extends any[], U extends any> = WithoutImplement<T,U>
우선 사용처에서 구현용 제네릭을 사용하지 못하게 하기 위해 구현과 실제 타입을 분리했다.
재귀적으로 타입을 구현했다.
Target이라는 제네릭을 선언해 U가 배열이면 해당 배열 요소들의 값을 제네릭(U[number])으로, U가 배열이 아니면 그냥 U를 Target으로 지정했다.
이후 맨 앞의 요소가 Target을 확장가능하면 재귀를 취한 나머지 요소를 반환하고, 그렇지 않다면 그 요소를 포함하고 재귀를 취한 나머지 요소를 반환한다.