Skip to content

How to remove Brand from zod Branded type #1994

Answered by afoures
afoures asked this question in Q&A
Discussion options

You must be logged in to vote

So in the end here is my current solution for this:

import { UnionToIntersection } from 'type-fest';

type Primitive =
  | string
  | number
  | boolean
  | undefined
  | null
  | bigint
  | Function
  | Symbol
  | Date
  | never
  | void;

type IterateOnTuple<T extends [...any[]]> = T extends [
  infer Head,
  ...infer Tail
]
  ? [Unbrand<Head>, ...IterateOnTuple<Tail>]
  : [];

type RemoveBrand<T> = T extends z.BRAND<infer Brand>
  ? T extends (
      | z.BRAND<Brand>
      | UnionToIntersection<{ [K in Brand]: z.BRAND<K> }[Brand]>
    ) &
      infer X
    ? RemoveBrand<X>
    : never
  : T;

type Unbrand<T> = T extends Primitive
  ? RemoveBrand<T>
  : T extends Promise<infer E>
  ? Pr…

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@afoures
Comment options

Comment options

You must be logged in to vote
1 reply
@afoures
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Fleshgrinder
Comment options

Answer selected by afoures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants