By combining Zod with Firestore’s `withConverter` API, you can enforce type safety at the boundary between your app and the database.
On this page
Introduction
Firebase - Firestore is a powerful, flexible NoSQL database — but with great flexibility comes the risk of subtle data shape bugs, especially in TypeScript projects where runtime and compile-time types can easily diverge.
Firestore’s SDKs is powerful, but they don’t enforce your TypeScript types at runtime.
That means you can write code like this:
type User = { name: string; age: number }const user: User = await getDoc(docRef).then((doc) => doc.data() as User)But if Firestore contains { name: "Alice" }, you won’t know until much later that age is missing.
This can lead to subtle bugs and runtime errors.
FirestoreDataConverter
Create a new project:
deno run -A npm:create-vite@latest zod-firebase --template react-ts --allow-scriptscd zod-firebasedeno install --allow-scripts --node-modules-dir --npm firebase tailwindcss @tailwindcss/viteZod lets you define runtime schemas that mirror your TypeScript interfaces.
import { z } from 'zod'
export const UserSchema = z.object({ name: z.string(), age: z.number(),})export type User = z.infer<typeof UserSchema>