I'm trying to create a library which extends the express
Response
with a property warn()
and when using the library I want to have the new property warn()
available.
This is 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/**/*"]
}
This is my @types/express/index.d.ts
:
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
}
}
}
src/registerwarn.ts
:
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
:
import { registerwarn } from './registerwarn'
export default [registerwarn()]
A test:
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()
})
})
Running tsc
returns these 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.
How can I get rid of these errors and make sure if somebody uses the library from npm
doesn't get these warnings as well?
I created a repro here.