Question3r
Question3r

Reputation: 3802

Proper Express Request object destructuring

I have an Express REST API and want to destructure the Request object. I use Typescript so I can see params is of type Dictionary<string>.

import { Request } from 'express';

export class UserService {
    public deleteUserById = async ({ params }: Request): Promise<void> => {
        const { id }: { id: number } = params;
    };
}

I get an error

Property 'id' is missing in type 'Dictionary' but required in type '{ id: number; }'.

The body for example is of type any. But how can I destructure the params object properly?

Upvotes: 0

Views: 987

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138367

id is definetly a string. Typescript is right.

Upvotes: 2

Related Questions