> For the complete documentation index, see [llms.txt](https://yamoo9.gitbook.io/typescript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yamoo9.gitbook.io/typescript/generics/type-inheritance.md).

# 타입 변수 상속

## 타입 변수 상속

**제네릭 타입 변수는 기존의 타입 변수를 상속할 수도 있습니다.** 이를 활용하면 입력 받을 타입을 지정할 수 있습니다. 예시 코드를 살펴보면 Model 클래스는 외부에서 사용할 때 타입을 지정할 수 있습니다. Model 클래스는 `add()` 인스턴스 메서드를 가지는데 아이템을 한 번에 하나씩 밖에 추가할 수 없습니다.&#x20;

사용에 편의를 더하기 위해 Model 객체 인스턴스 생성과 동시에 여러 아이템을 동시에 추가하는 `initializeModel()` 팩토리 함수를 작성할 때, `Model<U>`를 상속 받는 타입 `T`를 활용할 수 있습니다.

```typescript
// Model 클래스
class Model<T> {
  
  constructor(private _data:T[] = []) {}
  
  add(item:T):void {
    this._data.push(item);
  }

}

// Model 초기화 팩토리 함수
function initializeModel<T extends Model<U>, U>(C: new () => T, items:U[]):T {
  const c = new C();
  items.forEach(item => c.add(item));
  return c;
}

// 사용 예시
initializeModel<Model<string>, string>(Model, ['타입', '변수', '상속']);
```

## 실습 <a href="#practice" id="practice"></a>

{% embed url="<https://stackblitz.com/edit/ts-generic-type-inherit?embed=1&file=index.ts&view=editor>" %}

## 참고 <a href="#reference" id="reference"></a>

{% embed url="<https://www.typescriptlang.org/docs/handbook/generics.html>" %}
TypeScript - 제네릭
{% endembed %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://yamoo9.gitbook.io/typescript/generics/type-inheritance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
