TypeScript:Partial型とReadonly型を使って型からプロパティを除外する方法
TypeScriptで型からプロパティを除外する方法
Omit
型は、指定された型から特定のプロパティを除外した新しい型を作成します。
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = Omit<Person, "email">;
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
メリット:
- シンプルで分かりやすい
- 型推論が効く
- 除外したいプロパティの名前を個別に記述する必要がある
- ネストされた型の場合、深くネストしているプロパティを除外するのが煩雑になる
Pick 型と Exclude 型を使う
Pick
型は、指定されたプロパティのみを含む新しい型を作成します。 Exclude
型は、指定された型から特定の型を除外した新しい型を作成します。 これらの2つの型を組み合わせることで、特定のプロパティを除外した型を作成できます。
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = Pick<Person, Exclude<keyof Person, "email">>;
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
Pick
型とExclude
型を組み合わせる必要があるため、記述が少し冗長になる
インデックスシグネチャを使用することで、特定のプロパティを除外した型を作成できます。
interface Person {
[key: string]: any;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = {
[key: string]: key extends "email" ? never : any;
};
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
- 柔軟性の高い型定義ができる
- 記述が複雑になる
TypeScriptで型からプロパティを除外するには、いくつかの方法があります。 それぞれの方法にはメリットとデメリットがあるので、状況に合わせて最適な方法を選択してください。
Omit 型を使う
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = Omit<Person, "email">;
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
console.log(person.name); // "John Doe"
console.log(person.age); // 30
// console.log(person.email); // エラー: プロパティ `email` は型 `PersonWithoutEmail` に存在しません
// 他のプロパティも同様にアクセスできる
const person2: PersonWithoutEmail = {
name: "Jane Doe",
age: 31,
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
Pick 型と Exclude 型を使う
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = Pick<Person, Exclude<keyof Person, "email">>;
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
console.log(person.name); // "John Doe"
console.log(person.age); // 30
// console.log(person.email); // エラー: プロパティ `email` は型 `PersonWithoutEmail` に存在しません
// 他のプロパティも同様にアクセスできる
const person2: PersonWithoutEmail = {
name: "Jane Doe",
age: 31,
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
インデックスシグネチャを使う
interface Person {
[key: string]: any;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = {
[key: string]: key extends "email" ? never : any;
};
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
console.log(person.name); // "John Doe"
console.log(person.age); // 30
// console.log(person.email); // エラー: プロパティ `email` は型 `PersonWithoutEmail` に存在しません
// 他のプロパティも同様にアクセスできる
const person2: PersonWithoutEmail = {
name: "Jane Doe",
age: 31,
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
TypeScriptで型からプロパティを除外する他の方法
Partial
型は、すべてのプロパティをオプショナルなプロパティにした新しい型を作成します。
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型のすべてのプロパティをオプショナルにした新しい型
type PartialPerson = Partial<Person>;
const person: PartialPerson = {
name: "John Doe",
};
console.log(person.name); // "John Doe"
// console.log(person.age); // エラー: プロパティ `age` は型 `PartialPerson` に存在しません
// console.log(person.email); // エラー: プロパティ `email` は型 `PartialPerson` に存在しません
// オプショナルなプロパティに値を割り当てることもできる
const person2: PartialPerson = {
name: "Jane Doe",
age: 31,
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
この方法を使うと、すべてのプロパティをオプショナルにすることができます。 ただし、特定のプロパティのみを除外したい場合は、他の方法の方が効率的です。
Readonly
型は、すべてのプロパティを読み取り専用にした新しい型を作成します。
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型のすべてのプロパティを読み取り専用にした新しい型
type ReadonlyPerson = Readonly<Person>;
const person: ReadonlyPerson = {
name: "John Doe",
age: 30,
};
console.log(person.name); // "John Doe"
console.log(person.age); // 30
// person.name = "Jane Doe"; // エラー: プロパティ `name` は型 `ReadonlyPerson` において readonly です
// 読み取り専用のプロパティの値を取得することはできる
const person2: ReadonlyPerson = {
name: "Jane Doe",
age: 31,
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
マージ型を使って、型から特定のプロパティを除外することができます。
interface Person {
name: string;
age: number;
email: string;
}
// `Person` 型から `email` プロパティを除外した新しい型
type PersonWithoutEmail = Omit<Person, "email"> & {
email?: string;
};
const person: PersonWithoutEmail = {
name: "John Doe",
age: 30,
};
console.log(person.name); // "John Doe"
console.log(person.age); // 30
// console.log(person.email); // エラー: プロパティ `email` は型 `PersonWithoutEmail` に存在しません
// `email` プロパティはオプショナルなので、値を割り当てることもできる
const person2: PersonWithoutEmail = {
name: "Jane Doe",
age: 31,
email: "[email protected]",
};
console.log(person2.name); // "Jane Doe"
console.log(person2.age); // 31
console.log(person2.email); // "[email protected]"
この方法を使うと、特定のプロパティのみを除外することができます。 ただし、マージ型の記述が少し複雑になる場合があります。
- TypeScript開発
typescript