Cleanup array and Record types.

This commit is contained in:
João Vieira
2018-09-25 13:47:36 +01:00
parent 43fb400ad7
commit 35893a4d37
4 changed files with 35 additions and 29 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import { DefaultQuery } from "./router";
export interface RenderPageResponse {
buildManifest: Record<string, any>;
html?: string;
head?: Array<React.ReactElement<any>>;
head?: React.ReactElement<any>[];
}
export interface PageProps {
@@ -37,7 +37,7 @@ export interface NextDocumentContext<Q extends DefaultQuery = DefaultQuery> exte
* https://github.com/zeit/next.js/blob/7.0.0/server/document.js#L16
*/
export interface DefaultDocumentIProps extends RenderPageResponse {
styles?: Array<React.ReactElement<any>>;
styles?: React.ReactElement<any>[];
}
/**
+4 -10
View File
@@ -7,16 +7,10 @@ import {
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type AsyncComponent<P> = Promise<React.ComponentType<P>>;
type AsyncComponentLoader<P> = () => AsyncComponent<P>;
interface ModuleMapping {
[moduleName: string]: AsyncComponent<any>;
}
interface LoadedModuleMapping {
[moduleName: string]: React.ComponentType<any>;
}
type AsyncComponent<P = any> = Promise<React.ComponentType<P>>;
type AsyncComponentLoader<P = any> = () => AsyncComponent<P>;
type ModuleMapping = Record<string, AsyncComponent>;
type LoadedModuleMapping = Record<string, React.ComponentType>;
interface NextDynamicOptions<P = {}> extends Omit<LoadableOptions, "loading" | "modules"> {
modules?: () => ModuleMapping; // overridden
+2 -2
View File
@@ -3,6 +3,6 @@ import * as React from "react";
export function defaultHead(): JSX.Element[];
export default class Head extends React.Component {
static canUseDOM: boolean;
static peek(): Array<React.ReactElement<any>>;
static rewind(): Array<React.ReactElement<any>>;
static peek(): React.ReactElement<any>[];
static rewind(): React.ReactElement<any>[];
}
+27 -15
View File
@@ -1,4 +1,11 @@
import Document, { Enhancer, Head, Main, NextScript, NextDocumentContext, PageProps } from 'next/document';
import Document, {
Enhancer,
Head,
Main,
NextScript,
NextDocumentContext,
PageProps
} from "next/document";
import * as React from "react";
interface WithUrlProps {
@@ -11,12 +18,10 @@ class MyDoc extends Document<WithUrlProps> {
const _page = renderPage();
// with callback
const enhancer: Enhancer<PageProps, {}> = (App) => (props) => (<App />);
const enhancer: Enhancer<PageProps, {}> = App => props => <App />;
const { html, head, buildManifest } = renderPage(enhancer);
const styles = [
<style />
];
const styles = [<style />];
// Custom prop
const url = req!.url;
@@ -31,7 +36,7 @@ class MyDoc extends Document<WithUrlProps> {
<html>
<Head nonce="nonce" any="property" should="work" here>
<title>My page</title>
{ this.props.styles }
{this.props.styles}
</Head>
<body>
<Main />
@@ -44,12 +49,12 @@ class MyDoc extends Document<WithUrlProps> {
}
}
const renderPage: NextDocumentContext['renderPage'] = (enhancer) => ({
const renderPage: NextDocumentContext["renderPage"] = enhancer => ({
buildManifest: {},
chunks: { names: [], filenames: [] },
html: '',
html: "",
head: [<React.Fragment />],
errorHtml: '',
errorHtml: ""
});
interface PageInitialProps extends PageProps {
@@ -62,11 +67,18 @@ interface ProcessedInitialProps {
bar: boolean;
}
const enhancerExplicit: Enhancer<PageProps, {}> = (App) => (props) => (<App />);
const enhancerInferred = (App: React.ComponentType<ProcessedInitialProps>) => ({ foo, bar }: PageInitialProps) => (<App fooLength={foo.length} bar={!!bar} />);
const enhancerExplicit: Enhancer<PageProps, {}> = App => props => <App />;
const enhancerInferred = (App: React.ComponentType<ProcessedInitialProps>) => ({
foo,
bar
}: PageInitialProps) => <App fooLength={foo.length} bar={!!bar} />;
const explicitEnhancerRenderResponse = renderPage(enhancerExplicit);
const inferredEnhancerRenderResponse = renderPage(enhancerInferred);
const defaultedTypesRenderResponse = renderPage((App) => (props) => (<App url={props.url} />));
const defaultedTypesExtendedRenderResponse = renderPage((App) => (props) => (<App foo="bar" url={props.url} />));
const explicitTypesRenderResponseOne = renderPage<PageProps, {}>((App) => (props) => (<App />));
const explicitTypesRenderResponseTwo = renderPage<PageInitialProps, ProcessedInitialProps>((App) => ({ foo, bar }) => (<App fooLength={foo.length} bar={!!bar} />));
const defaultedTypesRenderResponse = renderPage(App => props => <App url={props.url} />);
const defaultedTypesExtendedRenderResponse = renderPage(App => props => (
<App foo="bar" url={props.url} />
));
const explicitTypesRenderResponseOne = renderPage<PageProps, {}>(App => props => <App />);
const explicitTypesRenderResponseTwo = renderPage<PageInitialProps, ProcessedInitialProps>(
App => ({ foo, bar }) => <App fooLength={foo.length} bar={!!bar} />
);