#Need help with type annotations (typescript)

1 messages · Page 1 of 1 (latest)

sullen jungle
#

Hi, can someone help me find the correct type annotations for some variables?
Here is the pastebin with the relevant code:
https://pastebin.com/FJmrB4BS

On line 70, I have tried doing the type typeof Blog, but that gave me this error on line 77:
Property 'save' does not exist on type 'Model<any, {}, {}, {}, any, any>'.

Same thing with both result arguments.

I have also tried console logging typeof newBlog and typeof result, and all of them gave object.

Thanks!

sullen jungle
#

please ping me if you have a response!

covert matrix
#

@sullen jungle I think some of this is the explicit type annotations all over the place. With TS you actually want to avoid these for the most part as it can override the defaults which are very intricate sometimes. My suggestion is to remove all of them and let the default types be added in.

#

example here:

app.get("/abyss", (req: Request, res: Response) => {
  res.render("abyss");
});

since you are within this app.get call, the type information already specifies the callback and its types, so they dont need to be explicitely added.

#

since app.get has a type signature somewhat like this (simplified)

get(path: string, cb: (req: Request, res: Response) => void)
#

with this signature TS can infer that your callback already is typed and doesnt need to be explicit

#

removing the Model<any> from your blog model in the model file, and removing the type annotation any from the new Blog call should give you the right typing

#

TS will infer the type for the model because it knows the return type of the mongoose.model function, same thing when doing new Blog, it knows that the return type of a class constructor will be an instance of Blog, no need to add the annotation in

sullen jungle
#

hiya @covert matrix! thanks for the explanations :)
i just have one more question - on lines 78 and 82, the compiler yells at me for not having type annotations. any idea what's happening there?
the errors in question are these:

Parameter 'result' implicitly has an 'any' type.
Parameter 'err' implicitly has an 'any' type.

thanks a lot!