Currently, I am in the process of developing TypeScript interfaces for each model that extends mongoose.Document.
import mongoose, { Document } from 'mongoose';
export interface IAccount extends Document {
_id: mongoose.Types.ObjectId;
name: string;
industry: string;
}
After defining the schema, it is then exported along with the interface:
export default mongoose.model<IAccount>('Account', accountSchema);
One issue encountered while testing with Jest is that simply creating an object with the necessary properties for the function being tested results in TypeScript raising errors due to missing fields.
function getData(account: IAccount){
return account;
}
const account = {
name: 'Test account name',
industry: 'test account industry'
}
getData(account);
The error message received is 'Argument of type '{ name: string; industry: string; }' is not assignable to parameter of type 'IAccount'. Type '{ name: string; industry: string; }' is missing the following properties from type 'IAccount': _id, increment, model, and 52 more.ts(2345)'
What would be the most straightforward method to create an object that meets TypeScript requirements for testing purposes?