I have been working on developing a library that extends the express
Response
object with a custom method called warn()
. I want this new warn()
method to be easily accessible when using the library.
Here is the configuration in my tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
},
"types": ["mocha"],
"typeRoots": ["@types"]
},
"include": ["src/**/*", "test/**/*"]
}
In the @types/express/index.d.ts
file:
import * as express from 'express'
import http from 'http'
export {}
declare global {
namespace Express {
export interface Response<ResBody = any>
extends http.ServerResponse,
express.Response {
warn(): this
}
}
}
The content of src/registerwarn.ts
is as follows:
export function registerwarn() {
return (_req: Request, res: Express.Response, next): void => {
res.warn = (): Express.Response => {
console.log("warning");
return res;
};
return next();
};
}
src/warnings.ts
imports and exports functions:
import { registerwarn } from './registerwarn'
export default [registerwarn()]
A sample test script located in test/warningtest.ts
:
import 'should'
import express, { Request, Response } from 'express'
import warnings from '../src/warnings'
describe('test', (): void => {
const app = express()
app.use(warnings)
app.get('/', (req: Request, res: Response) => {
res.status(200).warn().send({ some: 'content' })
})
it('should ', (done) => {
done()
})
})
Upon running tsc
, I encountered the following errors:
src/registerwarn.ts:3:9 - error TS2339: Property 'warn' does not exist on type 'Response'.
res.warn = (): Express.Response => {
~~~~
test/hello_tests.ts:9:21 - error TS2339: Property 'warn' does not exist on type 'Response<any>'.
res.status(200).warn().send({ some: 'content' })
~~~~
Found 2 errors.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I am seeking advice on how to resolve these errors and ensure that users of the library from npm
do not encounter similar issues. More details can be found in the repro provided here.