disclaimer: I am a bit uncertain about variance in general...
Here is the scenario I am facing:
// index.ts
import express from 'express';
import {Request, Response} from 'express';
const app = express();
app.use(handler);
interface BetterRequest extends Request {
foo: string;
}
function handler(req: BetterRequest, res: Response) {
req.foo = 'bar';
}
// tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
}
}
The error message I encounter is
[ts] Argument of type '(req: BetterRequest, res:Response) => void' is not assignable to parameter of type'RequestHandlerParams'. Type'(req: BetterRequest, res: Response) => void' is not assignabl... <!-- Here the content was shortened --> <p>I comprehend the meaning of the error and that I could either disable those warnings through editing the <code>tsconfig.json
file or by using a line comment. However, that's not my preferred solution.What would be the correct way to address this issue?
Is the below approach the only option?
// index.ts function handle(req: Request, res: Response) { const modifiedReq = req as BetterRequest; modifiedReq.foo = 'bar'; }