From 2e1fe62ed0f310b0eefb843663d55b9d68ae2cd5 Mon Sep 17 00:00:00 2001 From: Alessandro Giansanti Date: Tue, 22 Oct 2024 16:08:26 +0200 Subject: [PATCH] fix(validation-errors): type inference for branded, nullable, optional and array types (#284) --- .../next-safe-action/src/validation-errors.types.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/next-safe-action/src/validation-errors.types.ts b/packages/next-safe-action/src/validation-errors.types.ts index cd24dc9..8051d30 100644 --- a/packages/next-safe-action/src/validation-errors.types.ts +++ b/packages/next-safe-action/src/validation-errors.types.ts @@ -1,21 +1,24 @@ import type { Infer, InferIn, Schema } from "./adapters/types"; import type { Prettify } from "./utils.types"; +// Basic types and arrays. +type NotObject = number | string | boolean | bigint | symbol | null | undefined | any[]; + // Object with an optional list of validation errors. type VEList = Prettify<{ _errors?: string[] }>; // Creates nested schema validation errors type using recursion. type SchemaErrors = { - [K in keyof S]?: S[K] extends object | null | undefined ? Prettify> : VEList; + [K in keyof S]?: S[K] extends NotObject ? VEList : Prettify>; } & {}; /** * Type of the returned object when validation fails. */ export type ValidationErrors = S extends Schema - ? Infer extends object - ? Prettify>> - : VEList + ? Infer extends NotObject + ? VEList + : Prettify>> : undefined; /**