From 58bda1626fe56da649f2009ddab715ae0f77c902 Mon Sep 17 00:00:00 2001 From: Puneet Arora Date: Tue, 3 Mar 2020 10:43:22 -0800 Subject: [PATCH] [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 --- types/express/express-tests.ts | 12 ++++++++++++ types/express/index.d.ts | 3 ++- types/ghost-storage-base/ghost-storage-base-tests.ts | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/types/express/express-tests.ts b/types/express/express-tests.ts index b8b8c198e8..cf586435d9 100644 --- a/types/express/express-tests.ts +++ b/types/express/express-tests.ts @@ -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) => { + 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); diff --git a/types/express/index.d.ts b/types/express/index.d.ts index e60c0dce4f..034eb7869d 100644 --- a/types/express/index.d.ts +++ b/types/express/index.d.ts @@ -2,6 +2,7 @@ // Project: http://expressjs.com // Definitions by: Boris Yankov // China Medical University Hospital +// Puneet Arora // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -99,7 +100,7 @@ declare namespace e { interface Request

extends core.Request

{ } interface RequestHandler

extends core.RequestHandler

{ } interface RequestParamHandler extends core.RequestParamHandler { } - export interface Response extends core.Response { } + export interface Response extends core.Response { } interface Router extends core.Router { } interface Send extends core.Send { } } diff --git a/types/ghost-storage-base/ghost-storage-base-tests.ts b/types/ghost-storage-base/ghost-storage-base-tests.ts index fab2eff762..a263d2a167 100644 --- a/types/ghost-storage-base/ghost-storage-base-tests.ts +++ b/types/ghost-storage-base/ghost-storage-base-tests.ts @@ -41,5 +41,5 @@ storage.getSanitizedFileName('IMAGE.jpg'); // $ExpectType string storage.exists('tmp/123456.jpg', '/'); // $ExpectType Promise storage.save(image, '/'); // $ExpectType Promise -storage.serve(); // $ExpectType (req: Request, res: Response, next: NextFunction) => void +storage.serve(); // $ExpectType (req: Request, res: Response, next: NextFunction) => void storage.delete('tmp/123456.jpg', '/'); // $ExpectType Promise