TypeScript Utility Types完全ガイド:実務で即戦力になる使い方とパターン集
TypeScriptを使った開発において、型の定義と管理は品質を左右する重要な要素です。しかし、既存の型から部分的に新しい型を作成したい、特定のプロパティを除外したいといった場面は頻繁に発生します。こうした課題を解決するのがUtility Typesです。
本記事では、TypeScriptの標準に含まれるUtility Typesの実務的な使い方を、実際のビジネスロジックに基づいたコード例を交えて解説します。教科書的な説明ではなく、実際のプロジェクトで今日から使える知識をお届けします。
1. Utility Typesの基礎知識
Utility Typesは、既存の型を変換して新しい型を作成するための仕組みです。TypeScriptに組み込まれており、インポート不要で使用できます。
よく使われるUtility Typesは以下のとおりです:
Pick<T, K>:指定したプロパティのみを抽出Omit<T, K>:指定したプロパティを除外Partial<T>:すべてのプロパティをオプショナルにRequired<T>:すべてのプロパティを必須にRecord<K, T>:キーと値の型を指定したオブジェクト型Readonly<T>:すべてのプロパティを読み取り専用にExtract<T, U>:共通の型のみを抽出Exclude<T, U>:指定した型を除外
これらの機能は一見シンプルに見えますが、実務では型安全性を高めながらコードの重複を減らす強力なツールになります。
2. 業務でのリアルなユースケース
ユースケース1:ユーザー管理システムにおけるデータ処理
典型的なWebアプリケーションでは、ユーザー情報を複数の形態で扱う必要があります。例えば、データベースから取得したユーザー情報、API通信時のリクエスト/レスポンス、フロントエンドでの表示用データなど、シーンによって必要なプロパティが異なります。
Utility Typesを使わない場合、これらの型をすべて個別に定義することになり、メンテナンスが困難になります。
ユースケース2:フォーム入力のバリデーションと更新
ユーザー登録フォームでは、初回登録時と更新時で必須項目が異なることがあります。また、パスワードはレスポンスに含めたくないなど、セキュリティ要件でプロパティを制御する必要があります。
ユースケース3:機能フラグやコンフィグの型管理
アプリケーション全体で使われる設定値やフィーチャーフラグは、環境によって異なるプロパティを持つことがあります。基本型から柔軟に型を派生させることは、保守性を大きく向上させます。
3. 実装コード:実務パターン集
パターン1:ユーザー管理システムの型設計
まず、ユーザー情報の基本型を定義します。
// ユーザーの基本型定義
interface User {
id: number;
email: string;
password: string;
firstName: string;
lastName: string;
phone: string;
createdAt: Date;
updatedAt: Date;
isActive: boolean;
}
次に、Utility Typesを使って、異なる用途向けの型を派生させます。
// APIレスポンス用(パスワードは除外)
type UserResponse = Omit<User, 'password'>;
// ユーザー作成リクエスト(id、タイムスタンプは不要)
type CreateUserRequest = Omit<User, 'id' | 'createdAt' | 'updatedAt'>;
// ユーザー更新リクエスト(すべてのフィールドはオプション)
type UpdateUserRequest = Partial<Omit<User, 'id' | 'email' | 'createdAt'>>;
// 管理画面用(読み取り専用)
type UserReadonly = Readonly<UserResponse>;
// ユーザーリストの簡易表示用(名前とメールのみ)
type UserListItem = Pick<UserResponse, 'id' | 'firstName' | 'lastName' | 'email'>;
実際のAPI処理では、このように型を組み合わせて使用します。
// ユーザー作成API
async function createUser(request: CreateUserRequest): Promise<UserResponse> {
const user: User = {
...request,
id: generateId(),
createdAt: new Date(),
updatedAt: new Date(),
};
await saveUserToDatabase(user);
// パスワードを除外したレスポンスを返す
return omitPassword(user);
}
// ユーザー更新API
async function updateUser(userId: number, request: UpdateUserRequest): Promise<UserResponse> {
const existingUser = await getUserFromDatabase(userId);
// 更新可能なフィールドのみをマージ
const updatedUser: User = {
...existingUser,
...request,
updatedAt: new Date(),
};
await saveUserToDatabase(updatedUser);
return omitPassword(updatedUser);
}
// ユーザーリスト取得
async function listUsers(): Promise<UserListItem[]> {
const users = await getAllUsersFromDatabase();
return users.map(user => ({
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
}));
}
// ヘルパー関数
function omitPassword(user: User): UserResponse {
const { password, ...rest } = user;
return rest;
}
パターン2:フォーム検証とバリデーションルール
複雑なフォームでは、フィールドごとのバリデーションルールを定義する必要があります。Recordを使うと、型安全な方法で実装できます。
// フォームデータ型
interface RegistrationForm {
username: string;
email: string;
password: string;
confirmPassword: string;
age: number;
agreeToTerms: boolean;
newsletter: boolean;
}
// バリデーションルール型(各フィールドのルール配列)
type ValidationRules = Record<keyof RegistrationForm, Array<(value: any) => string | null>>;
// バリデーションルール実装
const validationRules: ValidationRules = {
username: [
(value: string) => !value ? 'ユーザー名は必須です' : null,
(value: string) => value.length < 3 ? '3文字以上で入力してください' : null,
(value: string) => /[^a-zA-Z0-9_]/.test(value) ? '英数字とアンダースコアのみです' : null,
],
email: [
(value: string) => !value ? 'メールアドレスは必須です' : null,
(value: string) => !/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(value) ? '有効なメールアドレスを入力してください' : null,
],
password: [
(value: string) => !value ? 'パスワードは必須です' : null,
(value: string) => value.length < 8 ? '8文字以上で入力してください' : null,
],
confirmPassword: [
(value: string, form?: RegistrationForm) => value !== form?.password ? 'パスワードが一致しません' : null,
],
age: [
(value: number) => value < 18 ? '18歳以上である必要があります' : null,
],
agreeToTerms: [
(value: boolean) => !value ? '利用規約に同意する必要があります' : null,
],
newsletter: [], // オプションフィールド
};
// フォーム検証関数
function validateForm(form: RegistrationForm): Record<keyof RegistrationForm, string[]> {
const errors: Record<keyof RegistrationForm, string[]> = {
username: [],
email: [],
password: [],
confirmPassword: [],
age: [],
agreeToTerms: [],
newsletter: [],
};
(Object.keys(validationRules) as Array<keyof RegistrationForm>).forEach(field => {
validationRules[field].forEach(rule => {
const error = rule(form[field], form);
if (error) {
errors[field].push(error);
}
});
});
return errors;
}
// 使用例
const formData: RegistrationForm = {
username: 'john_doe',
email: 'john@example.com',
password: 'SecurePass123',
confirmPassword: 'SecurePass123',
age: 25,
agreeToTerms: true,
newsletter: true,
};
const validationErrors = validateForm(formData);
console.log(validationErrors);
パターン3:環境設定とフィーチャーフラグ
アプリケーション設定は、環境によって異なるプロパティを持つことがあります。Utility Typesを使うと、基本設定から環境固有の設定を安全に拡張できます。
// 基本設定インターフェース
interface BaseConfig {
appName: string;
version: string;
logLevel: 'debug' | 'info' | 'warn' | 'error';
maxConnections: number;
requestTimeout: number;
}
// 環境固有の設定(追加プロパティ)
interface ProductionConfig extends BaseConfig {
apiEndpoint: string;
databaseUrl: string;
cacheTtl: number;
sentryDsn: string;
enableMetrics: true;
}
interface DevelopmentConfig extends BaseConfig {
mockApi: boolean;
localDatabasePath: string;
enableMetrics: false;
hotReload: boolean;
}
// 設定から読み取り専用版を作成
type ReadonlyProductionConfig = Readonly<ProductionConfig>;
type ReadonlyDevelopmentConfig = Readonly<DevelopmentConfig>;
// 環境による設定の作成
function getConfig(env: 'production' | 'development'): BaseConfig {
const baseConfig: BaseConfig = {
appName: 'MyApp',
version: '1.0.0',
logLevel: env === 'production' ? 'warn' : 'debug',
maxConnections: env === 'production' ? 100 : 10,
requestTimeout: 30000,
};
if (env === 'production') {
return {
...baseConfig,
apiEndpoint: 'https://api.example.com',
databaseUrl: process.env.DATABASE_URL || '',
cacheTtl: 3600,
sentryDsn: process.env.SENTRY_DSN || '',
enableMetrics: true,
} as ProductionConfig;
} else {
return {
...baseConfig,
mockApi: true,
localDatabasePath: './local.db',
enableMetrics: false,
hotReload: true,
} as DevelopmentConfig;
}
}
// 設定の一部をプログラムで変更する場合(Partial を使用)
type ConfigUpdate = Partial<BaseConfig>;
function updateConfig(config: BaseConfig, updates: ConfigUpdate): BaseConfig {
return { ...config, ...updates };
}
パターン4:APIレスポンスの型階層管理
複雑なRESTful APIでは、複数のエンドポイントが異なるレスポンス構造を持つことがあります。基本的なレスポンス型から、エンドポイント固有の型を派生させるパターンです。
// API基本レスポンス型
interface ApiResponse<T> {
success: boolean;
statusCode: number;
message: string;
data: T;
timestamp: string;
}
// ページネーション情報
interface PaginationMeta {
currentPage: number;
pageSize: number;
totalItems: number;
totalPages: number;
}
// リスト取得レスポンス
interface ListResponse<T> extends Omit<ApiResponse<T[]>, 'data'> {
data: T[];
meta: PaginationMeta;
}
// 具体的な使用例:商品データ
interface Product {
id: number;
name: string;
description: string;
price: number;
stock: number;
createdAt: string;
updatedAt: string;
}
// 商品取得レスポンス
type GetProductResponse = ApiResponse<Product>;
// 商品リスト取得レスポンス
type ListProductsResponse = ListResponse<Product>;
// 商品作成リクエスト(idとタイムスタンプは除外)
type CreateProductRequest = Omit<Product, 'id' | 'createdAt' | 'updatedAt'>;
// 商品更新リクエスト(すべてのフィールドはオプション、idは除外)
type UpdateProductRequest = Partial<Omit<Product, 'id' | 'createdAt' | 'updatedAt'>>;
// API実装例
class ProductApi {
async getProduct(id: number): Promise<GetProductResponse> {
const response = await fetch(`/api/products/${id}`);
return response.json();
}
async listProducts(page: number = 1, pageSize: number = 10): Promise<ListProductsResponse> {
const response = await fetch(`/api/products?page=${page}&pageSize=${pageSize}`);
return response.json();
}
async createProduct(request: CreateProductRequest): Promise<GetProductResponse> {
const response = await fetch('/api/products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(request),
});
return response.json();
}
async updateProduct(id: number, request: UpdateProductRequest): Promise<GetProductResponse> {
const response = await fetch(`/api/products/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(request),
});
return response.json();
}
}
4. よくある応用パターン
複数のUtility Typesを組み合わせた型定義
実務では、1つのUtility Typeだけでは要件を満たさないことがよくあります。複数を組み合わせることで、より複雑な要件に対応できます。
interface Article {
id: number;
title: string;
content: string;
authorId: number;
authorName: string;
tags: string[];
viewCount: number;
likeCount: number;
createdAt: Date;
updatedAt: Date;
publishedAt?: Date;
isDraft: boolean;
}
// 記事作成時:idと自動生成フィールド、ドラフトステータスは除外
type CreateArticleRequest = Omit<
Partial<Article>,
'id' | 'viewCount' | 'likeCount' | 'createdAt' | 'updatedAt'
> & {
title: string;
content: string;
authorId: number;
};
// ブログ表示用:著者IDは不要、内容は不要(リストなので)
type ArticleListItem = Pick<
Omit<Article, 'content' | 'authorId'>,
'id' | 'title' | 'authorName' | 'viewCount' | 'likeCount' | 'createdAt' | 'publishedAt'
>;
// キャッシュキー生成用:特定フィールドの組み合わせ
type ArticleCacheKey = Pick<Article, 'id' | 'updatedAt'>;
// 検索フィルタ:すべてのフィールドをオプションに
type ArticleSearchFilter = Partial<Pick<
Article,
'authorId' | 'tags' | 'isDraft' | 'publishedAt'
>>;
// 実装例
function buildArticleListItem(article: Article): ArticleListItem {
return {
id: article.id,
title: article.title,
authorName: article.authorName,
tags: article.tags,
viewCount: article.viewCount,
likeCount: article.likeCount,
createdAt: article.createdAt,
publishedAt: article.publishedAt,
};
}
function searchArticles(filter: ArticleSearchFilter): Promise<Article[]> {
// フィルタに基づいて検索
const query: Record<string, any> = {};
if (filter.authorId !== undefined) {
query.authorId = filter.authorId;
}
if (filter.tags !== undefined) {
query.tags = { $in: filter.tags };
}
if (filter.isDraft !== undefined) {
query.isDraft = filter.isDraft;
}
return fetchArticlesFromDatabase(query);
}
ジェネリック型とUtility Typesの組み合わせ
ジェネリック型とUtility Typesを組み合わせることで、再利用性の高い型定義が可能になります。
// 汎用的なエンティティ型
interface Entity {
id: number;
createdAt: Date;
updatedAt: Date;
}
// 汎用的なAPIレスポンス
interface ApiResponse<T> {
success: boolean;
data: T;
error?: string;
}
// 汎用的なペジネーションレスポンス
interface PaginatedResponse<T> extends ApiResponse<T[]> {
pagination: {
page: number;
pageSize: number;
total: number;
};
}
// 汎用的な作成リクエスト(id、タイムスタンプを除外)
type CreateRequest<T extends Entity> = Omit<T, 'id' | 'createdAt' | 'updatedAt'>;
// 汎用的な更新リクエスト(idを除外し、すべてオプション)
type UpdateRequest<T extends Entity> = Partial<Omit<T, 'id' | 'createdAt' | 'updatedAt'>>;
// 汎用的なリストアイテム(idとタイムスタンプのみ)
type ListItem<T extends Entity> = Pick<T, 'id' | 'createdAt' | 'updatedAt'> & Partial<Omit<T, 'id' | 'createdAt' | 'updatedAt'>>;
// 実装例:ユーザーとカテゴリーで統一インターフェースを使用
interface User extends Entity {
name: string;
email: string;
password: string;
}
interface Category extends Entity {
name: string;
description: string;
}
// 自動的に型が推論される
type CreateUserRequest = CreateRequest<User>;
type UpdateUserRequest = UpdateRequest<User>;
type UserListItem = ListItem<User>;
type CreateCategoryRequest = CreateRequest<Category>;
type UpdateCategoryRequest = UpdateRequest<Category>;
type CategoryListItem = ListItem<Category>;
// 汎用的なCRUD実装
class Repository<T extends Entity> {
async create(request: CreateRequest<T>): Promise<ApiResponse<T>> {
// 実装
return { success: true, data: {} as T };
}
async update(id: number, request: UpdateRequest<T>): Promise<ApiResponse<T>> {
// 実装
return { success: true, data: {} as T };
}
async list(page: number, pageSize: number): Promise<PaginatedResponse<T>> {
// 実装
return { success: true, data: [], pagination: { page, pageSize, total: 0 } };
}
}
条件付き型(Conditional Types)とUtility Typesの組み合わせ
TypeScriptの条件付き型とUtility Typesを組み合わせると、より高度な型操作が可能になります。
// 型が持つキーを条件付きで抽出
type OptionalPropertyNames<T> = {
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
}[keyof T];
type RequiredPropertyNames<T> = {
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
}[keyof T];
// オプショナルプロパティのみを抽出
type OptionalProperties<T> = Pick<T, OptionalPropertyNames<T>>;
// 必須プロパティのみを抽出
type RequiredProperties<T> = Pick<T, RequiredPropertyNames<T>>;
// 実装例
interface UserProfile {
id: number;
name: string;
email: string;
phone?: string;
address?: string;
bio?: string;
}
// オプショナルプロパティのみ:phone | address | bio
type OptionalUserProps = OptionalProperties<UserProfile>;
// 必須プロパティのみ:id | name | email
type RequiredUserProps = RequiredProperties<UserProfile>;
// フォーム検証:必須フィールドは入力必須、オプショナルフィールドは任意
function validateUserProfile(data: Partial<UserProfile>): {
errors: string[];
valid: boolean;
} {
const errors: string[] = [];
const requiredFields: (keyof RequiredUserProps)[] = ['id', 'name', 'email'];
requiredFields.forEach(field => {
if (!data[field]) {
errors.push(`${String(field)}は必須です`);
}
});
return {
errors,
valid: errors.length === 0,
};
}
5. 注意点とベストプラクティス
過度な型操作による可読性低下
Utility Typesは強力ですが、過度に使用すると型定義が複雑になり、保守性が低下します。
// ❌ 悪い例:複雑過ぎて理解しにくい
type ComplexType = Partial<
Pick<
Omit<
Required<User>,
'password'
>,
'id' | 'name' | 'email'
>
>;
// ✅ 良い例:中間型を経由して理解しやすく
type UserPublicInfo = Omit<User, 'password'>;
type UserPublicLimited = Pick<UserPublicInfo, 'id' | 'name' | 'email'>;
type UserPublicLimitedPartial = Partial<UserPublicLimited>;
型の安全性を損なわないようにする
Utility Typesを使う際は、型キャストを避け、型システムに任せることが重要です。
// ❌ 悪い例:型キャストで型安全性を失う
const user = getUserFromApi() as User;
// ✅ 良い例:適切なバリデーションと型定義
function isUser(obj: any): obj is User {
return (
typeof obj.id === 'number' &&
typeof obj.email === 'string' &&
typeof obj.password === 'string'
);
}
const userData = getUserFromApi();
if (isUser(userData)) {
// ここでは型安全にUserとして扱える
console.log(userData.email);
}
パフォーマンスへの配慮
複雑な型定義はTypeScriptのコンパイル時間に影響を与える可能性があります。特にジェネリック型を多用する場合は注意が必要です。
// 大規模な型定義の場合、中間型を活用してコンパイル時間を短縮
// ❌ 悪い例:深くネストされた型定義
type DeepNested<T> = T extends any ? (
T extends { [K in keyof T]: infer U } ? U : never
) : never;
// ✅ 良い例:段階的に型を組み立てる
type Step1<T> = Omit<T, 'password'>;
type Step2<T> = Pick<Step1<T>, 'id' | 'email'>;
type Step3<T> = Partial<Step2<T>>;
チーム内での型定義の統一
複数の開発者が携わるプロジェクトでは、Utility Typesの使用方法をチーム内で統一することが重要です。

