#Problem with controllers in express with typescript

1 messages · Page 1 of 1 (latest)

kind quail
#

I have a logout controller for admin that looks like this:

    req.admin.tokens = req.admin.tokens?.filter((token: any) => {
        return token.token !== req.token
    })
    await req.admin.save()
}```

And the route that uses the logout controller looks like this:
```adminRoute.post('/logout', adminController.logout)```


Now the error is in adminController.logout and it says: 
```Argument of type '(req: IReqAdmin, res: Response, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'Application<Record<string, any>>'.```


The IReqAdmin interface looks like this: 
```export interface IReqAdmin extends Request{
    token?: string,
    admin: IAdmin
} 
// The IAdmin interface: 
export interface IAdmin extends mongoose.Document{
    username: string,
    password: string,
    tokens?: {token?: string}[],
    generateAuthToken(): string
}```


How can I fix this?
Thanks in advance!
sharp geyser
#

You can't just put any type you would like instead of Request

#

You need to extend the Request type globally and you must make all properties optional. admin too