From 43fb400ad772042ec09cc4d670710b9d001345df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Vieira?= Date: Tue, 25 Sep 2018 13:35:22 +0100 Subject: [PATCH] Update dynamic types and tests. --- types/next/dynamic.d.ts | 67 ++++++++++++++--------- types/next/index.d.ts | 2 - types/next/test/next-dynamic-tests.tsx | 74 ++++++++++++++++++++------ 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/types/next/dynamic.d.ts b/types/next/dynamic.d.ts index 62d53f1e72..ebe0d4d9dd 100644 --- a/types/next/dynamic.d.ts +++ b/types/next/dynamic.d.ts @@ -1,29 +1,48 @@ import * as React from "react"; +import { + LoadableComponent, + CommonOptions as LoadableOptions, + LoadingComponentProps as LoadableLoadingComponentProps +} from "react-loadable"; -export interface DynamicOptions { - loading?: React.ComponentType; +type Omit = Pick>; + +type AsyncComponent

= Promise>; +type AsyncComponentLoader

= () => AsyncComponent

; + +interface ModuleMapping { + [moduleName: string]: AsyncComponent; +} + +interface LoadedModuleMapping { + [moduleName: string]: React.ComponentType; +} + +interface NextDynamicOptions

extends Omit { + modules?: () => ModuleMapping; // overridden + loading?: LoadableOptions["loading"]; // optional + loader?: AsyncComponentLoader

; // optional, overriden + render?: (props: P, loaded: LoadedModuleMapping) => React.ReactNode; // optional, overriden ssr?: boolean; - modules?( - props: TCProps & TLProps, - ): { [key: string]: Promise> }; - render?( - props: TCProps & TLProps, - modules: { [key: string]: React.ComponentType }, - ): void; + loadableGenerated?: { + webpack?: any; + modules?: any; + }; } -export class SameLoopPromise extends Promise { - constructor( - executor: ( - resolve: (value?: T) => void, - reject: (reason?: any) => void, - ) => void, - ); - setResult(value: T): void; - setError(value: any): void; - runIfNeeded(): void; -} -export default function( - componentPromise: Promise>, - options?: DynamicOptions, -): React.ComponentType; +type DynamicComponent

= React.ComponentType

& LoadableComponent; + +/** + * Overloaded dynamic function. + * https://github.com/zeit/next.js/blob/7.0.0/lib/dynamic.js#L55 + */ +declare function dynamic

( + options: AsyncComponent

| NextDynamicOptions

+): DynamicComponent

; +declare function dynamic

( + asyncModule: AsyncComponent

, + options: NextDynamicOptions

+): DynamicComponent

; + +export type LoadingComponentProps = LoadableLoadingComponentProps; +export default dynamic; diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 16f3fb7f02..d788ddd941 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -13,9 +13,7 @@ import * as http from "http"; import * as url from "url"; - import { Response as NodeResponse } from "node-fetch"; - import { SingletonRouter, DefaultQuery, UrlLike } from "./router"; declare namespace next { diff --git a/types/next/test/next-dynamic-tests.tsx b/types/next/test/next-dynamic-tests.tsx index 0b4cdde82d..72fa7e0651 100644 --- a/types/next/test/next-dynamic-tests.tsx +++ b/types/next/test/next-dynamic-tests.tsx @@ -1,25 +1,65 @@ -import dynamic, * as d from "next/dynamic"; import * as React from "react"; +import dynamic, { LoadingComponentProps } from "next/dynamic"; -// typically you'd use this with an esnext-style import() statement, but we'll make do without -interface DynamicComponentProps { +// You'd typically do this via import('./MyComponent') +interface MyComponentProps { foo: string; - bar: number; -} -async function getComponent() { - return (props: DynamicComponentProps) => ( -

- I'm an async component! {props.foo} {props.bar} -
- ); } +const MyComponent: React.StatelessComponent = () =>
I'm async!
; +const asyncComponent = Promise.resolve(MyComponent); -interface LoadingComponentProps { - baz: boolean; -} +// Examples from +// https://github.com/zeit/next.js/#dynamic-import -const DynamicComponent = dynamic(getComponent(), { - loading: (props: LoadingComponentProps) =>
Loading! {props.baz}
, +const LoadingComponent: React.StatelessComponent = ({ + isLoading, + error +}) =>

loading...

; + +// 1. Basic Usage (Also does SSR) +const DynamicComponent = dynamic(asyncComponent); +const dynamicComponentJSX = ; + +// 2. With Custom Loading Component +const DynamicComponentWithCustomLoading = dynamic(asyncComponent, { + loading: LoadingComponent +}); +const dynamicComponentWithCustomLoadingJSX = ; + +// 3. With No SSR +const DynamicComponentWithNoSSR = dynamic(asyncComponent, { + ssr: false }); -const jsx = ; +// 4. With Multiple Modules At Once +const HelloBundle = dynamic({ + modules: () => { + const components = { + Hello1: asyncComponent, + Hello2: asyncComponent + }; + + return components; + }, + render: (props, { Hello1, Hello2 }) => ( +
+

{props.foo}

+ + +
+ ) +}); +const helloBundleJSX = ; + +// 5. With plain Loadable options +const LoadableComponent = dynamic({ + loader: () => asyncComponent, + loading: LoadingComponent, + delay: 200, + timeout: 10000 +}); + +// 6. No loading +const DynamicComponentWithNoLoading = dynamic(asyncComponent, { + loading: () => null +});