[Express] [Type Update] Use generics for Response (#42662)

* use-generics-for-express-response-type: Use generics for Response in express

* use-generics-for-express-response-type: fixing ghost-storage-base-tests
This commit is contained in:
Puneet Arora 2020-03-03 10:43:22 -08:00 committed by GitHub
parent 83cc9b0beb
commit 58bda1626f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View File

@ -164,6 +164,18 @@ namespace express_tests {
// Params cannot be a custom type that does not conform to constraint
router.get<{ foo: number }>('/:foo', () => {}); // $ExpectError
// Response will default to any type
router.get("/", (req: Request, res: express.Response) => {
res.json({});
});
// Response will be of Type provided
router.get("/", (req: Request, res: express.Response<string>) => {
res.json();
res.json(1); // $ExpectError
res.send(1); // $ExpectError
});
app.use((req, res, next) => {
// hacky trick, router is just a handler
router(req, res, next);

View File

@ -2,6 +2,7 @@
// Project: http://expressjs.com
// Definitions by: Boris Yankov <https://github.com/borisyankov>
// China Medical University Hospital <https://github.com/CMUH>
// Puneet Arora <https://github.com/puneetar>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@ -99,7 +100,7 @@ declare namespace e {
interface Request<P extends core.Params = core.ParamsDictionary> extends core.Request<P> { }
interface RequestHandler<P extends core.Params = core.ParamsDictionary> extends core.RequestHandler<P> { }
interface RequestParamHandler extends core.RequestParamHandler { }
export interface Response extends core.Response { }
export interface Response<ResBody = any> extends core.Response<ResBody> { }
interface Router extends core.Router { }
interface Send extends core.Send { }
}

View File

@ -41,5 +41,5 @@ storage.getSanitizedFileName('IMAGE.jpg'); // $ExpectType string
storage.exists('tmp/123456.jpg', '/'); // $ExpectType Promise<boolean>
storage.save(image, '/'); // $ExpectType Promise<string>
storage.serve(); // $ExpectType (req: Request<ParamsDictionary>, res: Response, next: NextFunction) => void
storage.serve(); // $ExpectType (req: Request<ParamsDictionary>, res: Response<any>, next: NextFunction) => void
storage.delete('tmp/123456.jpg', '/'); // $ExpectType Promise<boolean>