Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support primitive-object-intersection types (branded primitive types) #277

Closed
wants to merge 2 commits into from

Conversation

aleclofabbro
Copy link
Contributor

@aleclofabbro aleclofabbro commented Sep 26, 2024

returnValidationErrors() typechecking fails when a property is a primitive-object-intersection type (typical case of branded type).

returnValidationErrors() refers to type SchemaErrors<S> for typings
the purpose of type SchemaErrors<S> as is:

type SchemaErrors<S> = {
	[K in keyof S]?: S[K] extends object | null | undefined 
	  ? Prettify<VEList & SchemaErrors<S[K]>> 
	  : VEList;
} & {};

is to map S properties conditionally:
if property S[K] is object | null | undefined maps to Prettify<VEList & SchemaErrors<S[K]>>
else (property S[K] is primitive) maps to VEList

the issue arises when property S[K] is a branded primitive type.
Branded primitive types are typically an intersection of a primitive with an object holding the brand
(e.g. type email = string & { [k in brand_prop_type]: unique symbol })

type SchemaErrors<S> mapping condition considers branded-primitive-types as object, mapping them incorrectly, and finally leading to returnValidationErrors() type errors

Proposed changes

the issue is resolved by simply reversing the condition:

type SchemaErrors<S> = {
  [K in keyof S]?: S[K] extends number | string | boolean | bigint
    ? VEList
    : Prettify<VEList & SchemaErrors<S[K]>>;
} & {};

this way any primitive - even when intersected - is eagerly catched and properly managed, keeping logic unaltered

Related issue(s) or discussion(s)

This is an improvement of a previous PR
#273

re #


…rors): support zod branded type

returnValidationErrors() typechecking fails when a property is a zod-branded-primitivwe-type, `type
SchemaErrors<S>` considers a zod-branded-primitivwe-type as an object instead as a primitive,
leading to a typecheck error. Fixed `type SchemaErrors<S>` definition to detect branded-primitives
and behave correctly
…t primitive-object-intersection

`returnValidationErrors()` typechecking fails when a property is a primitive&object intersection
type (typical case of branded type). Reversing the mappping condition of `type SchemaErrors<S>`
eagerly detects primitives - also if intersected - to properly manage them
Copy link

vercel bot commented Sep 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-safe-action-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 26, 2024 9:34pm

Copy link

vercel bot commented Sep 26, 2024

@aleclofabbro is attempting to deploy a commit to the Edoardo Ranghieri's projects Team on Vercel.

A member of the Team first needs to authorize it.

@aleclofabbro
Copy link
Contributor Author

HA!
It crossed my mind the primitive union may need to contain symbol too ! S[K] extends number | string | boolean | bigint | symbol

@TheEdoRan
Copy link
Owner

@aleclofabbro seems good to me! If the union needs to include symbol too, please push a commit with that and then I'll merge the PR, after testing it. Thank you!

@aleclofabbro
Copy link
Contributor Author

closed in favor of #284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants