TypeScript Schema
How to strongly type your entire Firebase database.
Usage
By default, Refirebase methods return any. You can get full end-to-end type safety by passing a schema interface to the Refirebase constructor.
Schema Definition
import { Refirebase } from 'refirebase';
// 1. Define your schema
type MySchema = {
users: {
id: string; // reserved for docId
name: string;
role: 'admin' | 'user';
age: number;
created_at?: number; // auto-managed
updated_at?: number; // auto-managed
};
posts: {
id: string;
title: string;
published: boolean;
authorId: string;
};
'users/posts': { // subcollections can be typed via slash
title: string;
}
};
// 2. Initialize with your schema
const refirebase = new Refirebase<MySchema>();
export const { db, auth } = refirebase;
// Now all methods are strictly typed!
const user = await db.firestore.get('users', { docId: '123' });
console.log(user?.name); // typed as string
// db.firestore.add('posts', { title: 123 }) // Error: Type 'number' is not assignable to type 'string'.
How it works
- The keys of the schema object become the allowed collection names (for both Firestore and Realtime Database).
- The values become the expected document shapes.
- The
idfield is automatically returned on reads and is optional on writes. - Subcollections can be typed using a string with a slash, e.g.,
'users/posts'.