I encountered an issue with my TypeScript Express project while attempting to implement a CSP Nonce using Helmet.
app.use(helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
scriptSrc: ["'self'", (req, res) => `'nonce-${res.locals.cspNonce}'`],
}
}));
Upon running the program, I received the following error message:
./index.ts:820
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts:17:59 - error TS2339: Property 'locals' does not exist on type 'ServerResponse'.
17 scriptSrc: ["'self'", (req, res) => `'nonce-${res.locals.nonce}'`],
~~~~~~
at createTSError (/home/frdiolin/WebstormProjects/Calender2.0/node_modules/ts-node/src/index.ts:820:12)
...
The interesting part is that the same code runs without any issues in JavaScript. Can someone shed some light on what might be causing this discrepancy?