Skip to content

Commit a9a2c8c

Browse files
committedJun 12, 2024··
fix(types): handle result undefined value at the action level
1 parent 16e5693 commit a9a2c8c

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed
 

‎apps/playground/src/app/(examples)/stateful-form/stateful-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const statefulAction = action
2222
await new Promise((res) => setTimeout(res, 1000));
2323

2424
return {
25-
prevName: prevResult?.data?.newName,
25+
prevName: prevResult.data?.newName,
2626
newName: parsedInput.name,
2727
};
2828
});

‎packages/next-safe-action/src/hooks.types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type HookResult<
1313
CVE,
1414
CBAVE,
1515
Data,
16-
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
16+
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
1717
fetchError?: string;
1818
};
1919

‎packages/next-safe-action/src/index.types.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,19 @@ export type SafeActionResult<
3434
Data = unknown,
3535
// eslint-disable-next-line
3636
NextCtx = unknown,
37-
> =
38-
| {
39-
data?: Data;
40-
serverError?: ServerError;
41-
validationErrors?: CVE;
42-
bindArgsValidationErrors?: CBAVE;
43-
}
44-
| undefined;
37+
> = {
38+
data?: Data;
39+
serverError?: ServerError;
40+
validationErrors?: CVE;
41+
bindArgsValidationErrors?: CBAVE;
42+
};
4543

4644
/**
4745
* Type of the function called from components with type safe input data.
4846
*/
4947
export type SafeActionFn<ServerError, S extends Schema | undefined, BAS extends readonly Schema[], CVE, CBAVE, Data> = (
5048
...clientInputs: [...bindArgsInputs: InferInArray<BAS>, input: S extends Schema ? InferIn<S> : void]
51-
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
49+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
5250

5351
/**
5452
* Type of the stateful function called from components with type safe input data.
@@ -66,7 +64,7 @@ export type SafeStateActionFn<
6664
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>,
6765
input: S extends Schema ? InferIn<S> : void,
6866
]
69-
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
67+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
7068

7169
/**
7270
* Type of the result of a middleware function. It extends the result of a safe action with

‎website/docs/execution/hooks/usestateaction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const statefulAction = actionClient
5252
await new Promise((res) => setTimeout(res, 1000));
5353

5454
return {
55-
prevName: prevResult?.data?.newName,
55+
prevName: prevResult.data?.newName,
5656
newName: parsedInput.name,
5757
};
5858
});

‎website/docs/types.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ export type SafeActionResult<
4646
Data = unknown,
4747
// eslint-disable-next-line
4848
NextCtx = unknown,
49-
> =
50-
| {
49+
> = {
5150
data?: Data;
5251
serverError?: ServerError;
5352
validationErrors?: CVE;
5453
bindArgsValidationErrors?: CBAVE;
5554
};
56-
| undefined;
5755
```
5856

5957
### `SafeActionFn`
@@ -70,7 +68,7 @@ export type SafeActionFn<
7068
Data
7169
> = (
7270
...clientInputs: [...bindArgsInputs: InferInArray<BAS>, input: S extends Schema ? InferIn<S> : void]
73-
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
71+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
7472
```
7573

7674
### `SafeStateActionFn`
@@ -91,7 +89,7 @@ export type SafeStateActionFn<
9189
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>,
9290
input: S extends Schema ? InferIn<S> : void,
9391
]
94-
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
92+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
9593
```
9694

9795
### `MiddlewareResult`
@@ -284,7 +282,7 @@ export type HookResult<
284282
CVE,
285283
CBAVE,
286284
Data,
287-
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
285+
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
288286
fetchError?: string;
289287
};
290288
```

0 commit comments

Comments
 (0)
Please sign in to comment.