1

I am not sure why typescript is barking at me when I try to use my own created type from express' types.

Argument of type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to parameter of type 'Context'.

Property 'req' is missing in type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' but required in type 'Context'.

The type signatures are identical, i'm not really sure what I'm doing wrong here. How can I pass req and res to getStatus? Routes.ts

import getStatus from "../routes/status";
 
export default function routes({ app }: Context): void {  
  app.get("/_status", ({ req, res }: Context) => getStatus(req, res));  
} // barking at me here                                    ^^^^^^^^

Status.ts

import { Context } from "../../utils/Context";
import express from "express";

export default async function getStatus({req}: Context, { res }: Context): Promise<Express.Response> {
//....
}

Context.ts

import { Request, Response, Application } from "express";
export type Context = {
  req: Request;
  res: Response;
  app: Application;
};

1 Answer 1

0

Basically the issue is that you're double destructuring. The anonymous function parameter already extracts the req and res from Context. The two fields are passed to getStatus, but it is expecting to get passed two Context parameters and extract req and res again.

I think the best thing to do is change the signature of getStatus so Request and Response are the types of the two parameters.

export default async function getStatus(req: Request, res: Response): Promise<Express.Response> {
   //....
}

Alternatively, just past context as one parameter.

app.get("/_status", getStatus)
export default async function getStatus({ req, res }: Context): Promise<Express.Response> {
   //....
}
Sign up to request clarification or add additional context in comments.

7 Comments

Why isn't object destructuring working from Context?
It is, but in your example you're passing the destructured parameters to getStatus. You're double destructuring.
since i'm setting the type to Context which contains these param-names, its not double? How else am I gonna use it? How is parameter gonna know what type-member to pick? I can't just do app.get("/", ({wtf, wtf2}:Context)
I expanded on my answer, hopefully it makes more sense. You already destructured Context in the app.get callback, you don't need to do it again. Either pass in the parameters, or pass the Context and destructure in the function, don't do it two places.
I think the best thing to do is change the signature of getStatus so Request and Response are the types of the two parameters., then what is the point of creating new type? What you are proposing is in my mind contradicting the languages features.
|

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.