I have a function like:
async queryAll(): Promise<Product[]> {
const response = await this.firestore.collection('products').get();
return response.docs.map(a => a.data());
}
And getting error:
Type 'DocumentData[]' is not assignable to type 'Product[]'. Type 'DocumentData' is missing the following properties from type 'Product': id, name
How can I add proper return typings for this method?
What can I see in firebase/index.ts.d
, get
function types looks like (I am using npm firebase package):
get(options?: GetOptions): Promise<QuerySnapshot<T>>;
But not sure how to apply this to my code.
valueof(str) method, it will return you a Boolean object and to see what it contains you will need to use booleanValue() method of the Boolean class as per Official Documentation Javadoc.
Build a DocumentReference to the document you want to update, then use the update() method on the DocumentReference to indicate only the fields to be added or changed. Pass it an object with only properties that match the fields to add or change.
I have found solution, need to use withConverter in order to add typings when retrieving data from firestore collections
Added working example, result
from dbQuery
function should have proper type i.g. Product[]
import firebase from 'firebase';
import { firebaseConfig } from '../firebaseConfig';
export interface Product {
name: string;
}
export const productConverter = {
toFirestore(product: Product): firebase.firestore.DocumentData {
return { name: product.name };
},
fromFirestore(
snapshot: firebase.firestore.QueryDocumentSnapshot,
options: firebase.firestore.SnapshotOptions
): Product {
const data = snapshot.data(options)!;
return { name: data.name }
}
};
async function dbQuery() {
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const response = await db.collection("products").withConverter(productConverter).get();
const result = response.docs.map(doc => {
const data = doc.data();
return data;
});
return result; // result type is Product[]
}
I find its very simple to use TypeScript's Type assertions feature for this.
await db.collection('products').get() as firebase.firestore.QuerySnapshot<Product>;
For single documents:
await db.collection('products').doc('12345').get() as firebase.firestore.DocumentSnapshot<Product>;
For snapshots:
db.collection('products')
.onSnapshot((snapshot: firebase.firestore.QuerySnapshot<Product>) => {
for (const doc of snapshot.docs) {
const product = doc.data();
}
});
When you call data()
on the document snapshot, its type will be of Product
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With