Drizzle produces wrong branded type using Zod
It appears using brands with Drizzle Zod doesn't work correct?
So effectively anytime you use your return value produced by
//? 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(),
})//? 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">;
....
}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;
}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()db.insert().returning()and pass the ID field (branded) to another param that expects the branded FileIdFileId, you get an error likeType '{ [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'.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'.