Skip to content

Commit d19326a

Browse files
committed
fix(types): correct validation error typing for arrays
fix #351
1 parent dead2f3 commit d19326a

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

packages/next-safe-action/src/utils.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export type Prettify<T> = {
55

66
// Returns type or promise of type.
77
export type MaybePromise<T> = Promise<T> | T;
8+
9+
// Returns type or array of type.
10+
export type MaybeArray<T> = T | T[];

packages/next-safe-action/src/validation-errors.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import type { Infer, InferIn, Schema } from "./adapters/types";
2-
import type { Prettify } from "./utils.types";
2+
import type { MaybeArray, Prettify } from "./utils.types";
33

44
// Basic types and arrays.
55
type NotObject = number | string | boolean | bigint | symbol | null | undefined | any[];
66

77
// Object with an optional list of validation errors.
8-
type VEList = Prettify<{ _errors?: string[] }>;
8+
type VEList<K = undefined> = K extends any[] ? MaybeArray<{ _errors?: string[] }> : { _errors?: string[] };
99

1010
// Creates nested schema validation errors type using recursion.
1111
type SchemaErrors<S> = {
12-
[K in keyof S]?: S[K] extends NotObject ? VEList : Prettify<VEList & SchemaErrors<S[K]>>;
12+
[K in keyof S]?: S[K] extends NotObject ? VEList<S[K]> : Prettify<VEList & SchemaErrors<S[K]>>;
1313
} & {};
1414

1515
/**
1616
* Type of the returned object when validation fails.
1717
*/
1818
export type ValidationErrors<S extends Schema | undefined> = S extends Schema
1919
? Infer<S> extends NotObject
20-
? VEList
20+
? Prettify<VEList>
2121
: Prettify<VEList & SchemaErrors<Infer<S>>>
2222
: undefined;
2323

0 commit comments

Comments
 (0)