Currently, I am retrieving data from Firestore:
getDocs(colRef).then(
(val) => {
const tempArray: Array<categoryFetchData> = val.docs.map((d) => {
const categoryData: {categoryName: string, color: string, createdAt: Timestamp} = d.data();
return {id: d.id, ...categoryData}
}
}
)
d.data()
is expected to have a return type of DocumentData
, but in this case, it will actually return
{categoryName: "someString", color: "someColor", createdAt: Timestamp.now()}
from the specific collection being fetched.
The function's return type is specified as Array<categoryFetchData>
type categoryFetchData = {
categoryName: string, color: string, createdAt: Timestamp, id: string
}
However, an error occurs indicating:
Type 'DocumentData' is missing the following properties from type '{ categoryName: string; color: string; createdAt: Timestamp; }': categoryName, color, createdAt
This error arises when attempting to spread d.data()
into the result.
To resolve this, one workaround is to do the following:
type ExpenseCategoryStruct = {categoryName: string; color: string; createdAt: Timestamp;};
const categoryData = d.data() as ExpenseCategoryStruct;
Is there a more efficient approach to handling this situation without needing to create a new variable for d.data()
and using as