arktypea
arktype9mo ago
log1st

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


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>>}"`,
  );


Maybe there's more clear way?
Was this page helpful?