0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

型定義で型の使い回し

Posted at

前提

CMSのデータをNext.js+TypeScriptで管理していたときに遭遇
ブログ記事、カテゴリ、タグと3つの型がある
カテゴリとタグはブログ記事の情報に含まれるため、使い回したい

コード

※カテゴリは単一、タグは複数

index.ts
export type blog = {
  id: string,
  createdAt: string,
  updatedAt: string,
  publishedAt: string,
  revisedAt: string,
  title: string,
  body: string,
  category: category, // categoryを使い回し
  tag: tag[], // tagを使い回し
}

export type category = {
  id: string,
  createdAt: string,
  updatedAt: string,
  publishedAt: string,
  revisedAt: string,
  name: string,
}

export type tag = {
  id: string,
  createdAt: string,
  updatedAt: string,
  publishedAt: string,
  revisedAt: string,
  name: string,
}

参考

型の再利用 | TypeScript入門『サバイバルTypeScript』

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?