I have a model with Activiy Interface and Mongo Schema, and a function that takes an ID and logs it. Then in my route I'm querying all activities from a given customer where the externalId is not undefined. With the returned array I call that function defined in my model, passing externalId as param.
But TypeScript keeps throwing: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'.
I already tried to wrap the call with an if statement checking if externalId && typeof externalId === 'number', but I can't make the error go away.
Model:
import { Schema, model, } from 'mongoose';
interface IActivity {
customer: string,
externalId?: number,
}
const activitySchema: Schema = new Schema({
Customer: {
type: String,
required: true,
minlength: 5,
maxlength: 50,
},
externalId: { type: Number, },
});
export function updateExternal(externalId: number): void {
console.log(externalId);
}
export const Activity = model<IActivityModel>('Activity', activitySchema);
Route:
const completeActivity = await Activity
.find({
'externalId': { '$ne': undefined, },
'customer': 'Foo Customer',
});
completeActivity.forEach((activity) => {
updateExternal(activity.externalId); // This is where the error appears
});