Get plain typescript type from enum

Hi, I am working on a NestJS + Drizzle app and I have the following in my schema:
export const userPermissionLevel = pgEnum("permissionLevel", ["admin","moderator","user","banned"]);
export const userPermissionLevel = pgEnum("permissionLevel", ["admin","moderator","user","banned"]);
Elsewhere in my code, I am writing a NestJS service that aims to resolve a user based on their current session cookie and I'd like to return the following type:
export interface UserMetadata {
hasEmail: boolean,
permissions: ???,
username: string
}
export interface UserMetadata {
hasEmail: boolean,
permissions: ???,
username: string
}
Is there a way to somehow make the permissions field always have a type in sync with the pgEnum? I tried InferColumnType but that clearly isn't correct Ideally in this scenario, I'd have permissions be the following type: "admin"|"moderator"|"user"|"banned" but of course derived somehow from the original enum declaration
3 Replies
Rph
RphOP16mo ago
Okay I figured out, though I'd appreciate feedback on if this is the "correct way" to do this:
permissions: typeof users.$inferSelect.permissionLevel
permissions: typeof users.$inferSelect.permissionLevel
francis
francis16mo ago
@rph I posted on this a while back, let me find it. @rph : https://discord.com/channels/1043890932593987624/1043890932593987627/1144690639397720246 you need something similar if you want to use the enum values as values in client code - since you can't import them in the browser bundle from the drizzle schema
Rph
RphOP16mo ago
My app is purely server side rendered, so I have no worries about client code, the whole thing is a Nest codebase
Want results from more Discord servers?
Add your server