Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions types/react-dom/canary.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @definitelytyped/no-self-import -- self-imports in module augmentations aren't self-imports */
/* eslint-disable @definitelytyped/no-declare-current-package -- The module augmentations are optional */
/**
* These are types for things that are present in the upcoming React 18 release.
*
Expand Down Expand Up @@ -35,3 +37,68 @@ declare module "react" {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface CacheSignal extends AbortSignal {}
}

declare const POSTPONED_STATE_SIGIL: unique symbol;
declare module "react-dom/static" {
/**
* This is an opaque type i.e. users should not make any assumptions about its structure.
* It is JSON-serializeable to be a able to store it and retrvieve later for use with {@link https://react.dev/reference/react-dom/server/resume `resume`}.
*/
interface PostponedState {
[POSTPONED_STATE_SIGIL]: never;
}

interface ResumeOptions {
nonce?: string;
signal?: AbortSignal;
onError?: (error: unknown) => string | undefined | void;
}

interface PrerenderResult {
postponed: null | PostponedState;
}
/**
* @see {@link https://react.dev/reference/react-dom/static/resumeAndPrerender `resumeAndPrerender` reference documentation}
* @version 19.2
*/
function resumeAndPrerender(
children: React.ReactNode,
postponedState: PostponedState,
options?: Omit<ResumeOptions, "nonce">,
): Promise<PrerenderResult>;

interface PrerenderToNodeStreamResult {
postponed: null | PostponedState;
}
/**
* @see {@link https://react.dev/reference/react-dom/static/resumeAndPrerenderToNodeStream `resumeAndPrerenderToNodeStream`` reference documentation}
* @version 19.2
*/
function resumeAndPrerenderToNodeStream(
children: React.ReactNode,
postponedState: PostponedState,
options?: Omit<ResumeOptions, "nonce">,
): Promise<PrerenderToNodeStreamResult>;
}

import { PostponedState, ResumeOptions } from "react-dom/static";
declare module "react-dom/server" {
/**
* @see {@link https://react.dev/reference/react-dom/server/resume `resume`` reference documentation}
* @version 19.2
*/
function resume(
children: React.ReactNode,
postponedState: PostponedState,
options?: ResumeOptions,
): Promise<ReactDOMServerReadableStream>;
/**
* @see {@link https://react.dev/reference/react-dom/server/resumeToPipeableStream `resumeToPipeableStream`` reference documentation}
* @version 19.2
*/
function resumeToPipeableStream(
children: React.ReactNode,
postponedState: PostponedState,
options?: ResumeOptions,
): Promise<PipeableStream>;
}
31 changes: 31 additions & 0 deletions types/react-dom/test/canary-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import React = require("react");
import ReactDOM = require("react-dom");
import ReactDOMClient = require("react-dom/client");
import ReactDOMServer = require("react-dom/server");
import ReactDOMStatic = require("react-dom/static");

function cacheSignalTest() {
const cacheSignal = React.cacheSignal;
Expand All @@ -15,3 +17,32 @@ function cacheSignalTest() {
}
}
}

async function resumeTest(children: React.ReactNode) {
const prerenderController = new AbortController();
setTimeout(prerenderController.abort, 1);
const { prelude, postponed } = await ReactDOMStatic.prerender(children, { signal: prerenderController.signal });

let stream: ReadableStream<Uint8Array>;
if (postponed !== null) {
const resumeController = new AbortController();
const reactStream = await ReactDOMServer.resume(children, postponed, {
nonce: "nonce",
signal: resumeController.signal,
onError(error) {
return (error as { digest?: string | undefined }).digest;
},
});
await reactStream.allReady;
// In a real app you'd also have to flush the prelude first
stream = reactStream;
} else {
stream = prelude;
}

await ReactDOMServer.resume(
children,
// @ts-expect-error Requires the opaque postponed state
{},
);
}
3 changes: 2 additions & 1 deletion types/react/ts5.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,8 @@ declare namespace React {
interface ComponentClass<P = {}, S = ComponentState> extends StaticLifecycle<P, S> {
// constructor signature must match React.Component
new(
props: P, /**
props: P,
/**
* Value of the parent {@link https://react.dev/reference/react/Component#context Context} specified
* in `contextType`.
*/
Expand Down