# 型の移動

TypeScriptのタイプシステムは非常に強力で、そこでは他の言語では不可能な方法で移動とスライシングが可能です。

これは、TypeScriptが、JavaScriptなどの高度に動的な言語でシームレスに作業できるように設計されているためです。ここでは、TypeScriptでタイプを移動するためのいくつかのトリックについて説明します。

これらに対する主な動機づけ：あなたは1つのものを変更し、他のものはすべて自動的に更新するだけで、うまく設計された制約システムのように、何かが壊れそうになると良い感じのエラーになります。

## Type + Valueの両方をコピーする

クラスを移動したい場合は、次の操作を行うことができます。

```typescript
class Foo { }
var Bar = Foo;
var bar: Bar; // ERROR: cannot find name 'Bar'
```

これはエラーです。なぜなら、`var`は`Foo`を\_変数\_宣言空間にコピーしただけなので、型の注釈として`Bar`を使うことができないからです。正しい方法は、`import`キーワードを使うことです。 \_namespaces\_や\_modules\_を使用している場合にのみ、`import`キーワードをこのように使用することができます(詳細は後で説明します)。

```typescript
namespace importing {
    export class Foo { }
}

import Bar = importing.Foo;
var bar: Bar; // Okay
```

この `import`トリックは、\_型と変数\_の両方で機能します。

## 変数の型を取り込む

`typeof`演算子を使用して、型の注釈で実際に変数を使用することができます。これにより、ある変数が別の変数と同じ型であることをコンパイラに伝えることができます。これを実証する例を以下に示します。

```typescript
var foo = 123;
var bar: typeof foo; // `bar` has the same type as `foo` (here `number`)
bar = 456; // Okay
bar = '789'; // ERROR: Type `string` is not `assignable` to type `number`
```

## クラスメンバのタイプを取得する

変数の型を取り込むのと同様に、単に型取得の目的で変数を宣言するだけです。

```typescript
class Foo {
  foo: number; // some member whose type we want to capture
}

// Purely to capture type
declare let _foo: Foo;

// Same as before
let bar: typeof _foo.foo; // `bar` has type `number`
```

## マジック文字列の種類を取得する

多くのJavaScriptライブラリとフレームワークは、生のJavaScript文字列を処理します。`const`変数を使用してその型を取り込むことができます。

```typescript
// Capture both the *type* and *value* of magic string:
const foo = "Hello World";

// Use the captured type:
let bar: typeof foo;

// bar can only ever be assigned to `Hello World`
bar = "Hello World"; // Okay!
bar = "anything else "; // Error!
```

この例では、`bar`はリテラルタイプ`'Hello World'`を持っています。これについては、 [リテラルタイプのセクション](https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html) を参照してください。

## キー名のキャプチャ

`keyof`演算子を使うと、ある型のキー名を取得できます。例えば。変数のキー名を`typeof`を使って最初に取得することでそれを取得することができます：

```typescript
const colors = {
  red: 'red',
  blue: 'blue'
}
type Colors = keyof typeof colors;

let color: Colors; // same as let color: "red" | "blue"
color = 'red'; // okay
color = 'blue'; // okay
color = 'anythingElse'; // Error: Type '"anythingElse"' is not assignable to type '"red" | "blue"'
```

上記の例のように、文字列列挙型+定数のようなものを簡単に作成することができます。


---

# Agent Instructions: 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:

```
GET https://typescript-jp.gitbook.io/deep-dive/type-system/moving-types.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
