Array of enums wrong type

Hello! I'm trying to migrate a column from enum to an array of enums, like this:
// old type,
role: userRoles("role").default("STUDENT").notNull(),
roles: userRoles("roles").array().$type<UserRole[]>().notNull(),
// old type,
role: userRoles("role").default("STUDENT").notNull(),
roles: userRoles("roles").array().$type<UserRole[]>().notNull(),
but when I try to insert any data, I get this type error: roles: [] as UserRole[],
Type '("UNKNOWN" | "STUDENT" | "TEACHER" | "SUPERUSER" | "ROOT")[]' is not assignable to type '"UNKNOWN" | "STUDENT" | "TEACHER" | "SUPERUSER" | "ROOT" | undefined'.
How does one do it?
1 Reply
benjick
benjickOP4w ago
I see, I was deriving the type from drizzle-zod, and it couldn't figure the shape of the key, so I had to provide it like this:
const userInsertSchema = createInsertSchema(users, {
roles: z.array(userRoleSchema),
});
const userInsertSchema = createInsertSchema(users, {
roles: z.array(userRoleSchema),
});

Did you find this page helpful?