I am trying to use CouchDB, and it's nano
npm library that has typescript types. I managed to insert documents, however I don't know how to type a response document. Here is the function that gets the data:
export const getAlarms = async (): Promise<IAlarm[]> => {
const list = await db.list({ include_docs: true });
const alarms = list.rows.map(alarm => {
if (alarm.doc) {
return Alarm.from(alarm.doc);}
});
return alarms;
};
Typescript throws an error at alarm.doc
:
[ts] Argument of type 'Document' is not assignable to parameter of type 'IAlarm'. Property 'name' is missing in type 'Document'.
I cannot find anywhere what to do at this point, how can I tell TS that the return document from this call is of my type and not a simple Document?
Edit: Just managed to fix it using alarm.doc as IAlarm
- how good of an approach is it?