• #TypeScript
  • #リファクタリング
  • #型定義
  • #DRY
開発完了

Article型の共通化

現状

以下の3ファイルで同じArticle interfaceが重複定義されている:

  1. app/pages/blog/index.vue
  2. app/components/BlogCalendar.vue
  3. app/components/TodoView.vue
interface Article {
  title: string
  description?: string
  path: string
  tags?: string[]
  publishedAt?: string
  updatedAt?: string
  category?: 'personal' | 'dev'
  todo?: 'active' | 'done' | 'canceled' | 'memo'
  project_name?: string
}

問題点

  • DRY原則違反(同じ定義が3箇所に存在)
  • フィールド追加時に3ファイルすべてを修正する必要がある
  • 不整合が発生するリスク

解決策

案1: types/article.ts に共通定義を作成

// app/types/article.ts
export interface Article {
  title: string
  description?: string
  path: string
  tags?: string[]
  publishedAt?: string
  updatedAt?: string
  category?: 'personal' | 'dev'
  todo?: 'active' | 'done' | 'canceled' | 'memo'
  project_name?: string
}

各コンポーネントでimport:

import type { Article } from '~/types/article'

案2: content.config.ts のスキーマから型を生成

Nuxt Contentのスキーマ定義から自動的に型を推論する方法。 ただし、設定が複雑になる可能性あり。

推奨

案1が最もシンプルで実装しやすい。

対象ファイル

  • app/types/article.ts (新規作成)
  • app/pages/blog/index.vue
  • app/components/BlogCalendar.vue
  • app/components/TodoView.vue

影響範囲

  • 型定義の変更のみ
  • 外部動作への影響なし