discriminated unionによるクラス型チェック
TypeScript におけるクラス型のチェック
型チェックは、変数やプロパティが期待される型と一致しているかどうかを確認する処理です。TypeScript では、コンパイル時に型チェックが行われます。型チェックによって、以下の問題を検出することができます。
- 型の間違い
- 存在しないプロパティへのアクセス
- 不適切な型の値の代入
クラス型は、オブジェクトの型を定義するために使用されます。クラス型には、プロパティとメソッドを定義することができます。
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
Typeguards は、変数やプロパティの型を判定するために使用されます。Typeguards は、以下の方法で使用することができます。
instanceof
演算子typeof
演算子- 型ガード関数
instanceof
演算子は、変数が特定のクラスのインスタンスかどうかを判定するために使用されます。
const person = new Person("John Doe", 30);
if (person instanceof Person) {
// person は Person クラスのインスタンスである
}
typeof
演算子は、変数の型を取得するために使用されます。
const person = new Person("John Doe", 30);
const type = typeof person; // "Person"
if (type === "Person") {
// person は Person 型である
}
型ガード関数は、変数の型を判定するために使用される関数です。
function isPerson(person: any): person is Person {
return person instanceof Person;
}
const person = new Person("John Doe", 30);
if (isPerson(person)) {
// person は Person 型である
}
クラス型チェックの例
以下の例は、クラス型チェックの使用方法を示しています。
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
function greetPerson(person: Person) {
console.log(`Hello, ${person.name}!`);
}
const person = new Person("John Doe", 30);
greetPerson(person); // "Hello, John Doe!"
// 型エラー: person は Person 型ではない
greetPerson({ name: "Jane Doe", age: 31 });
クラス型チェックは、TypeScript の型システムの重要な機能の一つです。クラス型チェックによって、コードの安全性と信頼性を向上させることができます。
// Person クラス
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
// greetPerson 関数
function greetPerson(person: Person) {
console.log(`Hello, ${person.name}!`);
}
// Person 型のインスタンス
const person = new Person("John Doe", 30);
// greetPerson 関数の呼び出し
greetPerson(person); // "Hello, John Doe!"
// 型エラー: person2 は Person 型ではない
const person2 = { name: "Jane Doe", age: 31 };
greetPerson(person2); // エラーが発生
このコードでは、Person
クラスと greetPerson
関数を定義しています。greetPerson
関数は、Person
型の引数を受け取り、その名前を出力します。
person
変数は Person
型のインスタンスとして作成され、greetPerson
関数の引数として渡されます。greetPerson
関数は person
の名前を出力します。
person2
変数は Person
型ではないオブジェクトとして作成されます。greetPerson
関数の引数として person2
を渡すと、型エラーが発生します。
このコードは、クラス型チェックの使用方法を示しています。クラス型チェックによって、コードの安全性と信頼性を向上させることができます。
クラス型チェックのその他の方法
interface Person {
name: string;
age: number;
}
function greetPerson(person: Person) {
console.log(`Hello, ${person.name}!`);
}
const person: Person = {
name: "John Doe",
age: 30,
};
greetPerson(person); // "Hello, John Doe!"
この例では、Person
インターフェースを定義しています。Person
インターフェースには、name
プロパティと age
プロパティが定義されています。
型エイリアスは、既存の型に新しい名前を付けるために使用されます。
type Person = {
name: string;
age: number;
};
function greetPerson(person: Person) {
console.log(`Hello, ${person.name}!`);
}
const person: Person = {
name: "John Doe",
age: 30,
};
greetPerson(person); // "Hello, John Doe!"
discriminated union は、複数の型のいずれかを持つ値を表すために使用されます。
type Person = {
type: "person";
name: string;
age: number;
};
type Animal = {
type: "animal";
species: string;
};
function greet(value: Person | Animal) {
if (value.type === "person") {
console.log(`Hello, ${value.name}!`);
} else if (value.type === "animal") {
console.log(`Hello, ${value.species}!`);
}
}
const person: Person = {
type: "person",
name: "John Doe",
age: 30,
};
const animal: Animal = {
type: "animal",
species: "dog",
};
greet(person); // "Hello, John Doe!"
greet(animal); // "Hello, dog!"
この例では、Person
discriminated union と Animal
discriminated union を定義しています。Person
discriminated union は、type
プロパティが "person" で、name
プロパティと age
プロパティを持つオブジェクトを表します。Animal
discriminated union は、type
プロパティが "animal" で、species
プロパティを持つオブジェクトを表します。
greet
関数は、Person
discriminated union または Animal
discriminated union の値を受け取り、type
プロパティに基づいて処理を分岐します。
function isPerson(value: any): value is Person {
return value.type === "person";
}
function greet(value: Person | Animal) {
if (isPerson(value)) {
console.log(`Hello, ${value.name}!`);
} else {
console.log(`Hello, ${value.species}!`);
}
}
const person: Person = {
type: "person",
name: "John Doe",
age: 30,
};
const animal: Animal = {
type: "animal",
species: "dog",
};
greet(person); // "Hello, John Doe!"
greet(animal); // "Hello, dog!"
この例では、isPerson
型ガード関数を定義しています。isPerson
型ガード関数は、`
typescript typechecking typeguards