mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
[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:
parent
83cc9b0beb
commit
58bda1626f
@ -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);
|
||||
|
||||
3
types/express/index.d.ts
vendored
3
types/express/index.d.ts
vendored
@ -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 { }
|
||||
}
|
||||
|
||||
@ -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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user