diff --git a/types/next/index.d.ts b/types/next/index.d.ts index addf881068..6996e8ec12 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -31,6 +31,34 @@ declare namespace next { type ServerConfig = NextConfig; // End Deprecated + /** + * HTTP request object (server only, non-export mode) + */ + type NextReq = http.IncomingMessage & CustomReq; + + /** + * HTTP request object (server only, `next export` mode) + * + * Note: We're using `Partial` here (instead of `{ url?: string }`) + * in order to avoid TS raising typedef errors + * when using it like `req && req.getHeaderNames ? req.getHeaderNames() : []`. + */ + type NextExportReq = Partial>; + + /** + * HTTP response object (server only, non-export mode) + */ + type NextResponse = http.ServerResponse; + + /** + * HTTP response object (server only, `next export` mode) + * + * Note: We're using `Partial` here (instead of `{}`) + * in order to avoid TS raising typedef errors + * when using it like `res && res.getHeaderNames ? res.getHeaderNames() : []`. + */ + type NextExportResponse = Partial; + /** * Context object used in methods like `getInitialProps()` * https://github.com/zeit/next.js/blob/7.0.0/server/render.js#L97 @@ -48,10 +76,16 @@ declare namespace next { query: Q; /** String of the actual path (including the query) shows in the browser */ asPath: string; - /** HTTP request object (server only) */ - req?: http.IncomingMessage & CustomReq; - /** HTTP response object (server only) */ - res?: http.ServerResponse; + /** + * HTTP request object (server only) + * Note: In `next export` mode, this will consist of only `{ url?: string }`. + */ + req?: NextReq | NextExportReq; + /** + * HTTP response object (server only) + * Note: In `next export` mode, this will be empty `{}` object. + */ + res?: NextResponse | NextExportResponse; /** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */ jsonPageRes?: NodeResponse; /** Error object if any error is encountered during the rendering */ diff --git a/types/next/test/next-document-tests.tsx b/types/next/test/next-document-tests.tsx index 3b9156aa08..5b19a6c8e9 100644 --- a/types/next/test/next-document-tests.tsx +++ b/types/next/test/next-document-tests.tsx @@ -134,13 +134,12 @@ const explicitTypesRenderResponseTwo = renderPage ({ foo, bar }) => ); -class MyDocumentWithCustomContext extends Document<{ example: string }> { - static async getInitialProps( - ctx: NextDocumentContext - ) { +class MyDocumentWithCustomContext extends Document<{ example: string; url: string }> { + static async getInitialProps(ctx: NextDocumentContext) { const initialProps = await Document.getInitialProps(ctx); const example = ctx.req ? ctx.req.customField : undefined; - return { ...initialProps, example }; + const url = ctx.req ? ctx.req.url : undefined; + return { ...initialProps, example, url }; } render() { @@ -151,6 +150,7 @@ class MyDocumentWithCustomContext extends Document<{ example: string }> {

{this.props.example}

+

{this.props.url}