Drizzle produces wrong branded type using Zod

It appears using brands with Drizzle Zod doesn't work correct?
//? Files *//
const fileIdSchema = z.string().uuid().brand<'FileId'>()
type FileId = z.infer<typeof fileIdSchema>
export const files = pgTable('files', {
    id: uuid()
        .primaryKey()
        .$defaultFn(() => uuidv7())
        .$type<FileId>(),
    name: varchar({ length: 255 }).notNull(),

})


type fileInternal = typeof files.$inferSelect
type fileInternal = {
    name: string;
    id: string & BRAND<"FileId">;
....
}


export const zodFilesInsertSchema = createInsertSchema(files)
export type FilesInsert = z.infer<typeof zodFilesInsertSchema>
type FilesInsert = {
    name: string;
    id: {
        [x: number]: string;
        length: number;
        [Symbol.iterator]: {};
        charAt: {};
          ...
        [BRAND]: {
            FileId: boolean;
        };
    } | undefined;

}



So effectively anytime you use your return value produced by db.insert().returning()and pass the ID field (branded) to another param that expects the branded FileId, you get an error like

Type '{ [x: number]: string; [BRAND]: { FileId: boolean; }; [Symbol.iterator]: {}; length: number; toString: {}; concat: {}; slice: {}; indexOf: {}; lastIndexOf: {}; includes: {}; at: {}; charAt: {}; charCodeAt: {}; ... 40 more ...; valueOf: {}; }' is not assignable to type 'string & BRAND<"FileId">'.
  Type '{ [x: number]: string; [BRAND]: { FileId: boolean; }; [Symbol.iterator]: {}; length: number; toString: {}; concat: {}; slice: {}; indexOf: {}; lastIndexOf: {}; includes: {}; at: {}; charAt: {}; charCodeAt: {}; ... 40 more ...; valueOf: {}; }' is not assignable to type 'string'.
Was this page helpful?