> For the complete documentation index, see [llms.txt](https://typescript-jp.gitbook.io/deep-dive/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://typescript-jp.gitbook.io/deep-dive/main-1/typeinstantiation.md).

# Type Instantiation

たとえば、ジェネリックパラメータを持つものがあるとします。クラス`Foo`です：

```typescript
class Foo<T>{
    foo: T;
}
```

特定の型の特殊バージョンを作成したいとします。このパターンは、項目を新しい変数にコピーし、ジェネリックを具体的な型に置き換えた型アノテーションを与えることです。例えば。 `Foo<number>`クラスが必要な場合です：

```typescript
class Foo<T>{
    foo: T;
}
let FooNumber = Foo as { new ():Foo<number> }; // ref 1
```

`ref 1`では、`FooNumber`は`Foo`と同じですが、`new`演算子で呼び出されたときに `Foo<Number>`のインスタンスを与えるものとして扱います。

## 継承

型アサーションパターンは、あなたが正しいことをする、と信じているという意味では安全ではありません。クラスの他の言語での共通パターンは継承を使うことです：

```typescript
class FooNumber extends Foo<number>{}
```

ここで注意しなければならないのは、デコレータをベースクラスで使用すると、継承されたクラスはベースクラスと同じ動作をしない可能性があることです(それは、もはやデコレータでラップされません)。

もちろん、クラスを特殊化していない場合でも、型強制/アサーションパターンがワークする方法を考える必要があります。なので、まず一般的なアサーションパターンを示します。

```typescript
function id<T>(x: T) { return x; }
const idNum = id as {(x:number):number};
```

> この[stackoverflowの質問](http://stackoverflow.com/a/34864705/390330)にインスパイアされました


---

# 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://typescript-jp.gitbook.io/deep-dive/main-1/typeinstantiation.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.
