diff --git a/types/easy-session/easy-session-tests.ts b/types/easy-session/easy-session-tests.ts index d707e89230..b4d3a3490f 100644 --- a/types/easy-session/easy-session-tests.ts +++ b/types/easy-session/easy-session-tests.ts @@ -49,7 +49,7 @@ app.get('/hasrole', function (req, res, next) { }); app.post('/setrole', function (req, res, next) { - req.session.setRole(req.query.role); + req.session.setRole(req.query.role as string); res.send(200); }); @@ -59,4 +59,4 @@ app.get('/getrole', function (req, res, next) { app.use(easySession.isLoggedIn()); app.use(easySession.isFresh()); -app.use(easySession.checkRole('user')); \ No newline at end of file +app.use(easySession.checkRole('user')); diff --git a/types/express-paginate/express-paginate-tests.ts b/types/express-paginate/express-paginate-tests.ts index ddd87ce441..86f1de4b6a 100644 --- a/types/express-paginate/express-paginate-tests.ts +++ b/types/express-paginate/express-paginate-tests.ts @@ -12,7 +12,7 @@ app.get('/users', async (req, res, next) => { return findAndCountAll({limit: req.query.limit, offset: req.skip}) .then(results => { const itemCount = results.count; - const pageCount = Math.ceil(results.count / req.query.limit); + const pageCount = Math.ceil(results.count / parseInt(req.query.limit as string, 10)); res.render('users/all_users', { users: results.rows, pageCount, @@ -20,7 +20,7 @@ app.get('/users', async (req, res, next) => { currentPageHref: paginate.href(req)(false, req.params), // Instead of exposing this to the html template, we'll test this here and pass a static number hasNextPages: paginate.hasNextPages(req)(pageCount), - pages: paginate.getArrayPages(req)(3, pageCount, req.query.page) + pages: paginate.getArrayPages(req)(3, pageCount, parseInt(req.query.page as string, 10)) }); }).catch(next); }); diff --git a/types/express-serve-static-core/express-serve-static-core-tests.ts b/types/express-serve-static-core/express-serve-static-core-tests.ts index ccd70e7a1f..b9eb7087c7 100644 --- a/types/express-serve-static-core/express-serve-static-core-tests.ts +++ b/types/express-serve-static-core/express-serve-static-core-tests.ts @@ -32,6 +32,17 @@ app.get<{ foo: string }>('/:foo', req => { // Params cannot be a custom type that does not conform to constraint app.get<{ foo: number }>('/:foo', () => {}); // $ExpectError +// Query can be a custom type +app.get<{}, any, any, {q: string}>('/:foo', req => { + req.query.q; // $ExpectType string + req.query.a; // $ExpectError +}); + +// Query will be defaulted to Query type +app.get('/:foo', req => { + req.query; // $ExpectType Query +}); + // Default types app.post("/", (req, res) => { req.params[0]; // $ExpectType string diff --git a/types/express-serve-static-core/index.d.ts b/types/express-serve-static-core/index.d.ts index dad20285af..74f11feac3 100644 --- a/types/express-serve-static-core/index.d.ts +++ b/types/express-serve-static-core/index.d.ts @@ -40,26 +40,30 @@ export interface ParamsDictionary { [key: string]: string; } export type ParamsArray = string[]; export type Params = ParamsDictionary | ParamsArray; -export interface RequestHandler
{ +// Return type of qs.parse, the default query parser (https://expressjs.com/en/api.html#app-settings-property). +export interface Query { [key: string]: string | string[] | Query | Query[]; } + +export interface RequestHandler
{ // tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2 - (req: Request
, res: Response , res: Response = (err: any, req: Request , res: Response =
+ (err: any, req: Request , res: Response
- = RequestHandler
- | ErrorRequestHandler
+export type RequestHandlerParams
+ = RequestHandler
+ | ErrorRequestHandler
| Array >;
export interface IRouterMatcher (path: PathParams, ...handlers: Array (path: PathParams, ...handlers: Array (path: PathParams, ...handlers: Array (path: PathParams, ...handlers: Array extends http.IncomingMessage, Express.Request {
+export interface Request extends http.IncomingMessage, Express.Request {
/**
* Return request header.
*
@@ -463,7 +467,7 @@ export interface Request ('/:foo', () => {}); // $ExpectError
+ // Query can be a custom type
+ router.get('/:foo', (req: express.Request<{}, any, any , {q: string}>) => {
+ req.query.q; // $ExpectType string
+ req.query.a; // $ExpectError
+ });
+
+ // Query will be defaulted to any
+ router.get('/:foo', (req: express.Request<{}>) => {
+ req.query; // $ExpectType Query
+ });
+
// Response will default to any type
router.get("/", (req: Request, res: express.Response) => {
res.json({});
diff --git a/types/express/index.d.ts b/types/express/index.d.ts
index 8c9246c5b3..128d308237 100644
--- a/types/express/index.d.ts
+++ b/types/express/index.d.ts
@@ -104,7 +104,7 @@ declare namespace e {
interface IRouterMatcher extends core.Request { }
+ interface Request extends core.Request { }
interface RequestHandler extends core.RequestHandler { }
interface RequestParamHandler extends core.RequestParamHandler { }
export interface Response