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
12 changes: 6 additions & 6 deletions types/react-dom/canary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ declare module "." {
function useFormStatus(): FormStatus;

function useFormState<State>(
action: (state: State) => Promise<State>,
initialState: State,
action: (state: Awaited<State>) => State | Promise<State>,
initialState: Awaited<State>,
permalink?: string,
): [state: State, dispatch: () => void];
): [state: Awaited<State>, dispatch: () => void];
function useFormState<State, Payload>(
action: (state: State, payload: Payload) => Promise<State>,
initialState: State,
action: (state: Awaited<State>, payload: Payload) => State | Promise<State>,
initialState: Awaited<State>,
permalink?: string,
): [state: State, dispatch: (payload: Payload) => void];
): [state: Awaited<State>, dispatch: (payload: Payload) => void];
}

declare module "./client" {
Expand Down
74 changes: 70 additions & 4 deletions types/react-dom/test/canary-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function preloadTest() {
ReactDOM.prefetchDNS("foo");
ReactDOM.prefetchDNS(
"bar", // @ts-expect-error prefetchDNS does not accept any options at the moment
{},
{}
);

// @ts-expect-error
Expand Down Expand Up @@ -128,7 +128,44 @@ function formTest() {
return state + 1;
}

const [state, dispatch] = useFormState(action, 1);
const [
// $ExpectType number
state,
dispatch,
] = useFormState(action, 1);

function actionExpectingPromiseState(state: Promise<number>) {
return state.then((s) => s + 1);
}

useFormState(
// @ts-expect-error
actionExpectingPromiseState,
Promise.resolve(1)
);
useFormState(
action,
// @ts-expect-error
Promise.resolve(1)
);
// $ExpectType number
useFormState<Promise<number>>(action, 1)[0];

useFormState(
async (
prevState: // only needed in TypeScript 4.9
// 5.0 infers `number` whereas 4.9 infers `0`
number
) => prevState + 1,
0
)[0];
// $ExpectType number
useFormState(
async (prevState) => prevState + 1,
// @ts-expect-error
Promise.resolve(0)
)[0];

return (
<button
onClick={() => {
Expand All @@ -145,7 +182,11 @@ function formTest() {
return state + 1;
}

const [state, dispatch] = useFormState(action, 1, "/permalink");
const [
// $ExpectType number
state,
dispatch,
] = useFormState(action, 1, "/permalink");
return (
<form action={dispatch}>
<span>Count: {state}</span>
Expand All @@ -154,12 +195,37 @@ function formTest() {
);
}

function Page3() {
function actionSync(state: number, type: "increment" | "decrement") {
return state + (type === "increment" ? 1 : -1);
}

const [
// $ExpectType number
state,
dispatch,
] = useFormState(actionSync, 1, "/permalink");
return (
<button
onClick={() => {
dispatch("decrement");
}}
>
count: {state}
</button>
);
}

function Page4() {
async function action(state: number, type: "increment" | "decrement") {
return state + (type === "increment" ? 1 : -1);
}

const [state, dispatch] = useFormState(action, 1, "/permalink");
const [
// $ExpectType number
state,
dispatch,
] = useFormState(action, 1, "/permalink");
return (
<button
onClick={() => {
Expand Down