”implements :実装する”は、
TypeScriptのクラスが特定のインターフェース(interface:結合)に準拠している
ことを明示するキーワード
です。
つまり、
インスタンス化したクラスは、”interfaceで定義された型”を実装しています
、とTypeScriptのコンパイラに伝えるキーワードです。
interfaceはコンパイラを通すだけなので、型を定義するだけの形です。
実装は持ちません。
多くの言語で見られる実装の前に、型だけを定義するのと同じです。implementsを使うことで、クラスがinterfaceで定義された型や構造を
正確に実装していることを保証します。
1. implementsの基本的な使い方
例
typescriptinterface Flyable {
fly(): void; // メソッドの型を定義
}
class Bird implements Flyable {
fly() {
console.log("The bird is flying!");
}
}
const bird = new Bird();
bird.fly(); // "The bird is flying!"
説明
interfaceの定義:Flyableは、fly()というメソッドを持つべき型を定義しています。
classでimplementsを使用:BirdクラスはFlyableをimplementsしており、fly()メソッドを実装する
必要があります。
- 型チェック:
Birdクラスがfly()メソッドを実装していなければ、TypeScriptが
コンパイルエラーを発生させます。
2. 複数のインターフェースをimplements
TypeScriptでは、1つのクラスで複数のインターフェースをimplementsすることができます。
例
typescriptinterface Swimmable {
swim(): void;
}
interface Flyable {
fly(): void;
}
class Duck implements Swimmable, Flyable {
swim() {
console.log("The duck is swimming!");
}
fly() {
console.log("The duck is flying!");
}
}
const duck = new Duck();
duck.swim(); // "The duck is swimming!"
duck.fly(); // "The duck is flying!"
説明
- 複数のインターフェースを使用:
DuckクラスはSwimmableとFlyableの両方をimplementsしています。
- すべてのメソッドを実装:
- クラスは、全てのインターフェースで定義されたプロパティやメソッドを
実装する必要があります。
- クラスは、全てのインターフェースで定義されたプロパティやメソッドを
3. インターフェースを部分的に実装しない場合
インターフェースで定義された全てのメソッドを実装しないと、コンパイルエラーになります。
例
typescriptinterface Walkable {
walk(): void;
}
class Robot implements Walkable {
// walk()メソッドを実装していないためエラー
}
エラー内容
goClass 'Robot' incorrectly implements interface 'Walkable'.
Property 'walk' is missing in type 'Robot' but required in type 'Walkable'.
4. implementsとクラスのextendsとの違い
| 特徴 | implements | extends |
|---|---|---|
| 役割 | クラスがインターフェースの構造に準拠することを保証 | クラスが他のクラスを継承し、その機能を引き継ぐ |
| 継承の対象 | interface | class |
| 複数指定 | 複数のインターフェースをimplementsできる | 単一のクラスしかextendsできない |
例:implementsとextendsの組み合わせ
typescriptinterface Walkable {
walk(): void;
}
interface Talkable {
talk(): void;
}
class Animal {
eat() {
console.log("The animal is eating.");
}
}
class Human extends Animal implements Walkable, Talkable {
walk() {
console.log("The human is walking.");
}
talk() {
console.log("The human is talking.");
}
}
const person = new Human();
person.eat(); // "The animal is eating."
person.walk(); // "The human is walking."
person.talk(); // "The human is talking."
説明
extendsとimplementsを同時に使用:HumanクラスはAnimalクラスを継承し、WalkableとTalkableインターフェースを
実装しています。
- 継承と実装の役割:
- 継承(
extends)は親クラスの機能(eatメソッド)を引き継ぎます。 - 実装(
implements)は、インターフェースで定義された
メソッド(walkとtalk)を定義します。
- 継承(
5. 実践的なimplementsの利用シーン
(1) クラスの型安全を確保
implementsを使うことで、クラスが必要な機能をすべて提供しているかどうかを
コンパイル時にチェックできます。
(2) インターフェースとしてのAPI定義
外部とのやり取りの型(APIのリクエストやレスポンス)をインターフェースで定義し、
それに基づいてクラスを実装します。
例: APIレスポンスの型チェック
typescriptinterface ApiResponse {
status: string;
data: any;
}
class SuccessResponse implements ApiResponse {
status: string;
data: any;
constructor(data: any) {
this.status = "success";
this.data = data;
}
}
class ErrorResponse implements ApiResponse {
status: string;
data: any;
constructor(message: string) {
this.status = "error";
this.data = { message };
}
}
const response: ApiResponse = new SuccessResponse({ id: 1 });
console.log(response.status); // "success"
結論
implementsはクラスが特定のインターフェースに準拠していることを保証するキーワードです。
これにより、型安全性が向上し、堅牢でメンテナンス性の高いコードを
記述することができます。extendsと併用して、クラスの継承と型定義の両方を柔軟に利用できます。


