0

I am trying to add typescript to my code and I do not seem to find how to infer some types. more precisely parameters of a callback.

Item.find({},(err, items) => {...} //missing type for callback parameters

"Item" is a Model from mongoose.

As you can see in the doc below, nothing specify the type of the callback parameters : https://mongoosejs.com/docs/api/model.html#model_Model-find

Am I supposed to just put "any" as a type ? Or is there some way I am missing out to find it?

1
  • Mongoose should have its own types... Commented Jan 6, 2023 at 19:38

1 Answer 1

1

I solved the problem by declaring an interface for the schema I was using.

interface IItem{...}
const itemSchema = new Schema<IItem> (...)
const Item = model<IItem>("Item", itemSchema)

So for the solution being :

Item.find({}, (err: String, items: Array<IItem>){...}

It was explained in the doc : https://mongoosejs.com/docs/typescript.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.