UNPKG

3.81 kBTypeScriptView Raw
1// Type definitions for @loadable/component 5.13
2// Project: https://github.com/smooth-code/loadable-components
3// Definitions by: Martynas Kadiša <https://github.com/martynaskadisa>
4// Daniel Playfair Cal <https://github.com/hedgepigdaniel>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.8
7
8import * as React from "react";
9
10export interface DefaultImportedComponent<Props> {
11 default: React.ComponentType<Props>;
12}
13
14export type DefaultComponent<Props> = React.ComponentType<Props> | DefaultImportedComponent<Props>;
15
16/**
17 * Synchronous function that returns the component from the
18 * imported module.
19 *
20 * The default works with default exports, both CommonJS or ESM
21 */
22export type ComponentResolver<Props, Module = DefaultComponent<Props>> = (
23 module: Module,
24 props: Props,
25) => React.ComponentType<Props>;
26
27export interface OptionsWithoutResolver<Props> {
28 cacheKey?(props: Props): any;
29 fallback?: JSX.Element | undefined;
30 ssr?: boolean | undefined;
31}
32
33export interface Options<Props, Module = DefaultComponent<Props>> extends OptionsWithoutResolver<Props> {
34 resolveComponent?: ComponentResolver<Props, Module> | undefined;
35}
36
37export interface OptionsWithResolver<Props, Module = DefaultComponent<Props>> extends OptionsWithoutResolver<Props> {
38 resolveComponent: ComponentResolver<Props, Module>;
39}
40
41export interface LoadableReadyOptions {
42 namespace?: string | undefined;
43}
44
45export interface LoadableComponentMethods<Props> {
46 preload(props?: Props): void;
47 load(props?: Props): Promise<React.ComponentType<Props>>;
48}
49
50export interface ExtraComponentProps {
51 fallback?: JSX.Element | undefined;
52}
53
54export type LoadableComponent<Props> =
55 & React.ComponentType<Props & ExtraComponentProps>
56 & LoadableComponentMethods<Props>;
57
58export interface ExtraClassComponentProps<Component extends React.ComponentClass> extends ExtraComponentProps {
59 ref?: React.LegacyRef<InstanceType<Component>> | undefined;
60}
61
62export type LoadableClassComponent<Component extends React.ComponentClass> =
63 & React.ComponentType<
64 React.ComponentProps<Component> & ExtraClassComponentProps<Component>
65 >
66 & LoadableComponentMethods<React.ComponentProps<Component>>;
67
68export type LoadableLibrary<Module> =
69 & React.ComponentType<{
70 fallback?: JSX.Element | undefined;
71 children?: ((module: Module) => React.ReactNode) | undefined;
72 ref?: React.Ref<Module> | undefined;
73 }>
74 & Module
75 & LoadableComponentMethods<object>;
76
77declare function lib<Props, Module>(
78 loadFn: (props: Props) => Promise<Module>,
79 options?: OptionsWithoutResolver<Props>,
80): LoadableLibrary<Module>;
81
82declare function loadableFunc<Props, Module = DefaultComponent<Props>>(
83 loadFn: (props: Props) => Promise<Module>,
84 options: OptionsWithResolver<Props, Module>,
85): LoadableComponent<Props>;
86
87declare function loadableFunc<Props>(
88 loadFn: (props: Props) => Promise<DefaultComponent<Props>>,
89 options?: OptionsWithoutResolver<Props>,
90): LoadableComponent<Props>;
91
92declare function loadableFunc<Component extends React.ComponentClass<any>>(
93 loadFn: (props: React.ComponentProps<Component>) => Promise<Component | { default: Component }>,
94 options?: Options<React.ComponentProps<Component>, Component>,
95): LoadableClassComponent<Component>;
96
97declare const loadable: typeof loadableFunc & { lib: typeof lib };
98
99export default loadable;
100
101export namespace lazy {
102 function lib<Module>(loadFn: (props: object) => Promise<Module>): LoadableLibrary<Module>;
103}
104
105export function lazy<Props>(loadFn: (props: Props) => Promise<DefaultComponent<Props>>): LoadableComponent<Props>;
106
107export function loadableReady(done?: () => any, options?: LoadableReadyOptions): Promise<void>;
108
\No newline at end of file