#Better Way to Infer Type Parameter?

2 messages · Page 1 of 1 (latest)

hoary sparrow
#

Overview

In my Node API, I have a bunch of repository and service classes that all do the same things for basic CRUD operations using Prisma. Rather than copy/paste a bunch of boilerplate, I've tried to make a base class that the entities can extend that holds these operations.

The solution that I've arrived at looks pretty nasty, and I hardly understand it myself. Snippet (with my shoddy notes to self) and full links are below.

Question

Does anyone know of a more elegant way to get the DTO type arguments in the repository...

export abstract class BaseRepository<PrismaType, DTO> {

from the service base class?

// Create a new type named RepositoryType that takes a Repo type parameter.
// Repo must be type that extends (inherits from) BaseRepository.
type RepositoryTypes<Repo extends BaseRepository<any, any>> =
  // Now to define the type:
  // Use the "extands" conditional to access the type parameters from
  // BaseRepository - P and D - and cram them into a type object.
  Repo extends BaseRepository<infer P, infer D> ? { prisma: P; dto: D } : never;

export abstract class BaseService<Repo extends BaseRepository<any, any>> {

Links

GitHub

Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.

GitHub

Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.

GitHub

Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.

GitHub

Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.

novel ridge
#

Hi
Would you mind also posting this on the Prisma Discord? I'm really interested in a good answer to this question — I’ve had to do some rather 'cursed' things myself to get type inference flowing from Prisma, through the backend, and into the frontend via tRPC
Thanks in advance