last day (15 days later) » 

13:33
1
A: Type not being checked in custom response structure in Express

Tobias S.You could change your type definiton to this: declare namespace Express { interface Response { showDefaultErrorPage?: any showErrorPage?: any success?: (options: ApiResponse) => Response<any, Record<string, any>, number> error?: (options: ApiResponse) => Response<any, Record<str...

Returning: return res.status(status).json({ status, message: message || getStatusMessage(status) || 'Server error' })
So the ApiResponse interface must go in the custom type definiton file, and get removed from middleware file?
@Ayan - Well you are gonna need this interface in both files. So you may export it and import it in the other file.
Could you please help me understand why would I need in both places?
@Ayan - You need in the declare namespace Express { ... for the type of the argument in the function but also in the responseHelper because you are using it as the type of the argument of res.success too.
ooh! I understand. BTW, I get the following: Cannot invoke an object which is possibly 'undefined'.ts(2722) on res.error also on res.success
13:33
@Ayan - Yes, you specified that success and error are optional, so a Response might or might not have them. You would either need to check if they are defined before calling them, or remove the question mark.
Got it. Getting to understand how it works. One last thing, Response<any, Record<string, any>, number> does this mimic the ApiInterface once again?
@Ayan - I just checked what type res.status(status).json returns as this is also the return type of your function if you just return its result.
The app crashes with the error: TS2551: Property 'error' does not exist on type 'Response<any, Record<string, any>>'. Did you mean 'errored'? Is it because error function is being set in a middleware?
@Ayan - On which line does this error happen?
The error is thrown when I run the app. This error happens wherever I have used the custom functions error, success etc.
13:33
@Ayan - That does not look like a runtime error. That error is definitely produced by TypeScript during compile time. How are you compiling the app? Just tsc or something with webpack?
@Tobias Hi
for the dev, nodemon is being used
"scripts": {
    "build": "npx tsc",
    "start": "npm run build && node build/server.js",
    "dev": "nodemon src/server.ts",
    "generate:keys": "ts-node src/utils/generateKeys.ts",
    "lint": "eslint \"**/*.ts\"",
    "lint:fix": "eslint \"**/*.ts\" --fix",
    "prepare": "husky install"
  },
Running typescript 4.8.2
 
5 hours later…
18:10
@Ayan - Does the error change if you move the declare namespace in the same file as the middleware?
18:29
Gotta try that
give me a moment
@TobiasS. yes it changes
18:44
@Ayan - I reproduced your project locally. See the update to the answer.
We need to let TypeScript infer most of the types. The types of express are often too verbose to write them out manually
I added some comments in the code for important details
Alright, I am going to make the appropriate changes and see if it works
BTW, should we let Express to handle types on their own
I mean let them infer
If you put the type Request on the res parameter, it is technically the wrong type as the correct type would be Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>> but this is too annoying to write out.
Where do we get to see these types?
If you dont give an explicit type and hover over res and req you can see these types
Aaah! found it
Currently TS feels a lot like some sort of cryptic language
Ok so according to the implementation you showed in the answer, it needs to be placed in the entry file?
or can I place it in the middleware
18:56
Yes, or otherwise make app available in the middleware file.
Changes in place, but I get this one error
The function needs to be passed to app.use directly. I sadly did not find a type for the function that can be used here. Passing it directly to app.use will let TypeScript infer the types
In that case everything needs to go into one single file
You could also pass the app object to another file to use it is there
Why cant we use the custom definition file to do the merging?
19:09
what merging
Declaration merging?
We are using it succesfully but that is not the issue with the function. Express does not seem to expose a type for the function. Using Request and Response does not work well with the merging. So we need to let TypeScript infer the types by passing it to app.use
You could post this as a seperate question. Maybe someone else knows the correct type for this function
@TobiasS. I understand your point now
@TobiasS. I could definitely do that. Also I can try and dig into issues of express on github just in case

last day (15 days later) »