Install
npx skillscat add geckou/project-starter/new-type Install via the SkillsCat registry.
SKILL.md
new-type
packages/shared/src/types/ に型定義を追加する。
手順
- ユーザーに型の名前と用途を確認する
packages/shared/src/types/index.tsに型を追加する- ファイルが大きくなったら分割する(例:
types/user.ts,types/chat.ts) - 分割した場合は
types/index.tsから re-export する
- ファイルが大きくなったら分割する(例:
packages/shared/src/index.tsから既にexport * from './types'しているので、追加の export は不要
テンプレート
基本的な型
export type EntityName = {
id: string
createdAt: Date
updatedAt: Date
}Firestore ドキュメント型(日時は DateLike)
Firestore の日時は書き込むときは Date、読み出すと Timestamp になる。
どちらかに寄せた型にすると、もう一方で実行時に落ちる(Timestamp に getTime() は無い)。packages/shared は両方を受ける DateLike を export しているので、それを使う。
packages/shared/src/index.ts のルートバレルは環境非依存(型・純ユーティリティ・定数)のみを
export する方針のため、firebase/firestore の Timestamp は import せず、toDate() を持つ構造型として表現している。
// DateLike は同じ index.ts で宣言済み(import は不要)
export type EntityName = {
id: string
createdAt: DateLike
updatedAt: DateLike
}値として使うときは toDate()(@geckou/shared が export)を通す。
import { toDate } from '@geckou/shared'
const createdAt = toDate(entity.createdAt) // Date | nullAPI レスポンス型
// 既存の ApiResponse<T> を活用する
import type { ApiResponse } from './index'
// 例: ユーザー一覧の API レスポンス
type UsersResponse = ApiResponse<User[]>ルール
types/には環境依存パッケージ(firebase,zustand等)を import しない
(ルートバレルpackages/shared/src/index.tsは環境非依存のみを export する方針。
functions が@geckou/sharedを import しても firebase client SDK の型解決が不要になる)- 型名はパスカルケース(例:
ChatRoom,UserProfile) - Firestore のドキュメント型の日時は
DateLike(Date | { toDate(): Date })にする。
読み出した値を使うときはtoDate()を通す(Doc/ アプリ形式の 2 型に分けない) interfaceではなくtypeを使う(プロジェクトの規約)- シングルクォートを使う
- 既存の
ApiResponse<T>を積極的に活用する