Typescript interface conformance with firestore queries

Given that doc.data() always returns a DocumentData, which is just {[field: string]: any}, I don't think there's any better way to turn that into a Payment than casting. You could write:

const data = doc.data();
const payment = {amount: doc.amount, /*and so on for the remaining members*/};

without a cast, but I don't think that's better in practical terms. It would make more sense to me for DocumentData to be just any (like the return of JSON.parse to give one example); you could file an issue on the project to propose that.


Not sure if this helps, but the way I think of it is that the type you have in your program and the types you send and recieve from Firestore are different types.

So, typescript has this 'Partial' feature that takes an interface and makes all of the fields optional. That way even if the object isn't complete, it will still conform and provide you with intellisense.

So, I do this : export type dataUpdate<T> = Partial<T>;

Then, I do : const userPayload: dataUpdate<User> = snapshot/typecase as User