I have an array that might include null or undefined values
const uncertainObjectIds = ['5fc657037ed3e03d506e1e25', null, '5fc657037ed3e03d506e1e26', undefined]
If uncertainObjectIds does include any null or undefined values I will throw an error. However, if it does not and I am sure that the array is full of defined values and no undefined values I wanna use it.
const doNullValuesExistInsideObjectIds = uncertainObjectIds.some(
(objectIdOrNull) => !objectIdOrNull,
);
if (doNullValuesExistInsideObjectIds) {
throw new UserInputError(
'All Users must have ObjectIds',
);
}
const usersCollection = await getCollection('users');
const originalUsers = await usersCollection.find({
_id: { $in: uncertainObjectIds },
});
but TypesSript gives me an error.
Type '(ObjectId | null | undefined)[]' is not assignable to type 'ObjectId[]'
What is the best way to handle this use case inside TypeScript?
uncertainObjectIds.filter(f => !!f)