Skip to content

A library of typescript interfaces that extend existing firebase classes, adding type safety and a better autocomplete experience.

License

Notifications You must be signed in to change notification settings

chaseholdren/typesafe-firestore

Repository files navigation

typesafe-firestore

npm version

A library of typescript interfaces that extend existing firebase classes, adding type safety and a better autocomplete experience.

Installation

npm install typesafe-firestore --save-dev

Usage

You most likely want to import TypedCollectionReference and create your collections as shown below.

import firebase from 'firebase/app';
import 'firebase/firestore';
import { TypedCollectionReference } from 'typesafe-firestore';

interface Author {
  penName: string;
  shortBiography: string;
  posts: string[];
}

const AuthorCollection = firebase.firestore().collection('authors') as TypedCollectionReference<Author>;

And then you can use your typesafe collection the same ways you would use the regular firestore library.

// OK
AuthorCollection.add({
  penName: '',
  shortBiography: '',
  posts: [],
});

// TS2322: Type 'number' is not assignable to type 'string'.
AuthorCollection.add({
  penName: 11,
  shortBiography: '',
  posts: [],
});

// OK
AuthorCollection.where('penName', '<=', 'Barfunk');

// TS2345: Argument of type '"realName"' is not assignable to parameter of type '"penName" | "shortBiography" | "posts" | FieldPath'.
AuthorCollection.where('realName', '<=', 'Barfunk');

License

typesafe-firestore is MIT licensed.

About

A library of typescript interfaces that extend existing firebase classes, adding type safety and a better autocomplete experience.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published