Infinite Learner
Infinite Learner

Reputation: 316

How to infer type of callback parameters for typescript

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?

Upvotes: 0

Views: 45

Answers (1)

Infinite Learner
Infinite Learner

Reputation: 316

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

Upvotes: 1

Related Questions