union of strings & Record as Generic argument

I'm using prisma and it generates Object for runtime and union of that object keys as type. Due to that I'm unable to use it in my type schemas:
// prisma
export const GameTransactionType: {
bet: 'bet',
win: 'win',
betWin: 'betWin',
rollback: 'rollback',
promo: 'promo',
end: 'end'
};

export type GameTransactionType = (typeof GameTransactionType)[keyof typeof GameTransactionType]

// my code
const myType = type(`"${GameTransactionType.bet}"`) // "bet" is not assignable to type "'bet' is unresolvable "

// type(GameTransactionType.bet) - same
// prisma
export const GameTransactionType: {
bet: 'bet',
win: 'win',
betWin: 'betWin',
rollback: 'rollback',
promo: 'promo',
end: 'end'
};

export type GameTransactionType = (typeof GameTransactionType)[keyof typeof GameTransactionType]

// my code
const myType = type(`"${GameTransactionType.bet}"`) // "bet" is not assignable to type "'bet' is unresolvable "

// type(GameTransactionType.bet) - same
I've tried to manage it using scopes but looks like it is due to conflict of types. My current hack is:
export const enumToLiteral = <T extends string | Record<string, unknown>>(
t: T,
) =>
type(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
`"${t as unknown as Exclude<T, Record<string, unknown>>}"`,
);
export const enumToLiteral = <T extends string | Record<string, unknown>>(
t: T,
) =>
type(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
`"${t as unknown as Exclude<T, Record<string, unknown>>}"`,
);
Maybe there's more clear way?
2 Replies
ssalbdivad
ssalbdivad3w ago
I think you just want type.enumerated(...Object.keys(GameTransactionType))
ssalbdivad
ssalbdivad3w ago
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime

Did you find this page helpful?