> For the complete documentation index, see [llms.txt](https://taewoongs-organization.gitbook.io/jtwjs-dev-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://taewoongs-organization.gitbook.io/jtwjs-dev-wiki/dev_note/fp/undefined-4.md).

# 제너릭 타입 활용하기

## Generic

> 타입의 대한 중복을 쉽게 다루게 해주는 도구

다른 모든 타입에 대해서 **구현은 동일**한 함수가 있다면, 이러한 **중복을 일반화시켜 제거**한 후 하나의 함수로 만드는 방법이 제너릭을 사용하는 것

* **타입을 매개변수화** 시켜서 어떤 타입이라도 같은 동작을 하게 만듦
* 제너릭으로 만들어진 타입은 **타입을 입력받으면 타입을 반환하는 타입에 대한 함수**
* 즉, 타입을 생성하는 함수로 생각할 수 있다.

```typescript
const idNumber = (n: number): number => {
  return n;
}

const idString = (s: string): string => {
  return x;
}

const idBoolean = (b: boolean): boolean => {
  return b;
}

const id = <T>(x: T): T => {
  return x;
}
```

아무리 타입이 달라지더라도 구현부는 동일하기 때문에 일반화가 가능

```typescript
const compose = <A, B, C>(g: (z: B) => C, f: (y: A) => B) => (x: A) => {
  return g(f(x));
}
// <A, B, C>((B) => C, (A) => B) => (A) => C
```
