Infer Type from PgEnum

Hey, I'd like to infer a PgEnum to a TypeScript Type. So something like this:
const eventTypeEnum = pgEnum('event_type', [
'training',
'matchday',
'tournament',
'event',
'labour'
]);

// get the following as a result
type EventType = 'training' | 'matchday' | 'tournament' | 'event' | 'labour'
const eventTypeEnum = pgEnum('event_type', [
'training',
'matchday',
'tournament',
'event',
'labour'
]);

// get the following as a result
type EventType = 'training' | 'matchday' | 'tournament' | 'event' | 'labour'
How could I achieve this?
2 Replies
Maston
Maston2y ago
you can do something like this i guess?
const EVENTS = ['training', /*...*/] as const;
const eventTypeEnum = pgEnum('event_type', EVENTS);
type EventType = typeof EVENTS[number];
const EVENTS = ['training', /*...*/] as const;
const eventTypeEnum = pgEnum('event_type', EVENTS);
type EventType = typeof EVENTS[number];
henrikfiedler
henrikfiedlerOP2y ago
great call! thank you very much!
Want results from more Discord servers?
Add your server