Skip to content

Commit

Permalink
Merge pull request #1619 from pmkroeker/master
Browse files Browse the repository at this point in the history
Add types for Suspense/lazy
  • Loading branch information
marvinhagemeister committed May 12, 2019
2 parents a2790ac + cc2bb15 commit 8e36628
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compat/src/index.d.ts
Expand Up @@ -9,6 +9,8 @@ export import createRef = preact.createRef;
export import Fragment = preact.Fragment;
export import createElement = preact.createElement
export import cloneElement = preact.cloneElement
export import Suspense = preact.Suspense;
export import lazy = preact.lazy;

export declare const version: string;

Expand Down Expand Up @@ -66,6 +68,8 @@ declare const _default: {
forwardRef: typeof forwardRef,
unstable_batchedUpdates: typeof unstable_batchedUpdates,
Children: Children,
Suspense: typeof Suspense,
lazy: typeof lazy,
};

export default _default;
12 changes: 12 additions & 0 deletions src/index.d.ts
Expand Up @@ -231,4 +231,16 @@ declare namespace preact {
interface PreactContext<T> extends Context<T> {}

function createContext<T>(defaultValue: T): Context<T>;

//
// Suspense/lazy
// -----------------------------------
function lazy<T>(loader: () => Promise<{default: T}>): T;

interface SuspenseProps {
children?: ComponentChildren;
fallback: ComponentChildren;
}

abstract class Suspense extends Component<SuspenseProps> {}
}
23 changes: 23 additions & 0 deletions test/ts/lazy.tsx
@@ -0,0 +1,23 @@
import {
createElement,
Component,
} from "../..";

export interface LazyProps {
isProp: boolean;
}

interface LazyState {
forState: string;
}
export default class IsLazyComponent extends Component<LazyProps, LazyState> {
render ({ isProp }: LazyProps) {
return (
<div>{
isProp ?
'Super Lazy TRUE' :
'Super Lazy FALSE'
}</div>
)
}
}
42 changes: 42 additions & 0 deletions test/ts/suspense-test.tsx
@@ -0,0 +1,42 @@
import "mocha";
import { expect } from "chai";
import {
createElement,
Component,
Suspense,
lazy,
} from "../..";

interface LazyProps {
isProp: boolean;
}

const IsLazyFunctional = (props: LazyProps) =>
<div>{
props.isProp ?
'Super Lazy TRUE' :
'Super Lazy FALSE'
}</div>

const FallBack = () => <div>Still working...</div>;
/**
* Have to mock dynamic import as import() throws a syntax error in the test runner
*/
const componentPromise = new Promise<{default: typeof IsLazyFunctional}>(resolve=>{
setTimeout(()=>{
resolve({ default: IsLazyFunctional});
},800);
});

/**
* For usage with import:
* const IsLazyComp = lazy(() => import('./lazy'));
*/
const IsLazyFunc = lazy(() => componentPromise);

// Suspense using lazy component
class SuspensefulFunc extends Component {
render() {
return <Suspense fallback={<FallBack/>}><IsLazyFunc isProp={false} /></Suspense>
}
}

0 comments on commit 8e36628

Please sign in to comment.