Unverified Commit 2eee8766 authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by GitHub

Add types for getInitialProps (#7162)

* Added types for pages
parent f66546f9
...@@ -8,8 +8,8 @@ import { BaseRouter } from './router/router' ...@@ -8,8 +8,8 @@ import { BaseRouter } from './router/router'
/** /**
* Types used by both next and next-server * Types used by both next and next-server
*/ */
export type NextComponentType<C extends BaseContext = any, P = any, CP = {}> = ComponentType<CP> & { export type NextComponentType<C extends BaseContext = IContext, IP = {}, P = {}> = ComponentType<P> & {
getInitialProps?(context: C): Promise<P>, getInitialProps?(context: C): Promise<IP>,
} }
export type DocumentType = NextComponentType<DocumentContext, DocumentInitialProps, DocumentProps> export type DocumentType = NextComponentType<DocumentContext, DocumentInitialProps, DocumentProps>
...@@ -123,7 +123,7 @@ export function isResSent(res: ServerResponse) { ...@@ -123,7 +123,7 @@ export function isResSent(res: ServerResponse) {
return res.finished || res.headersSent return res.finished || res.headersSent
} }
export async function loadGetInitialProps<C extends BaseContext, P = any, CP = {}>(Component: NextComponentType<C, P, CP>, ctx: C): Promise<P | null> { export async function loadGetInitialProps<C extends BaseContext, IP = {}, P = {}>(Component: NextComponentType<C, IP, P>, ctx: C): Promise<IP | null> {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (Component.prototype && Component.prototype.getInitialProps) { if (Component.prototype && Component.prototype.getInitialProps) {
const message = `"${getDisplayName(Component)}.getInitialProps()" is defined as an instance method - visit https://err.sh/zeit/next.js/get-initial-props-as-an-instance-method for more information.` const message = `"${getDisplayName(Component)}.getInitialProps()" is defined as an instance method - visit https://err.sh/zeit/next.js/get-initial-props-as-an-instance-method for more information.`
......
import React from 'react' import React from 'react'
import { IContext } from 'next-server/dist/lib/utils';
// Extend the React types with missing properties // Extend the React types with missing properties
declare module 'react' { declare module 'react' {
...@@ -18,3 +19,12 @@ declare module 'react' { ...@@ -18,3 +19,12 @@ declare module 'react' {
global?: boolean; global?: boolean;
} }
} }
export type NextPageContext = IContext
export type NextPage<P = {}> = {
(props: P): JSX.Element;
getInitialProps?(ctx: NextPageContext): Promise<P>;
}
export { IContext }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment