prismock icon indicating copy to clipboard operation
prismock copied to clipboard

`PrismockClient` type is incorrect

Open electrovir opened this issue 10 months ago • 3 comments

Problem

This type cast is incorrect: https://github.com/morintd/prismock/blob/1b3baeb75b4749cedd0482479a178e39a1f37a2b/src/lib/client.ts#L116

It's intersecting the PrismockData interface as a static interface, not an instance interface.

What I mean by that is that with the current types, this is a type error (but not a run-time error):

const client = new PrismockClient();
client.getData();

while this is not a type error but is a run-time error:

const client = new PrismockClient();
PrismockClient.getData();

Solution

You basically need to unwrap and rewrap the PrismaClient constructor so you can modify the returned instance type. There might be a more elegant way of doing this, but this is working for me:

import type {Constructor} from 'type-fest';

type RealPrismockClient = Constructor<
    PrismaClient & PrismockData,
    ConstructorParameters<typeof PrismaClient>
>;

Note that if you don't want to include the type-fest dependency, the Constructor type that it exports is as follows:

export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;

electrovir avatar Apr 08 '24 12:04 electrovir

Hello @electrovir , thanks a lot! I'll think about the best way to fix that and try to get something done in the next few days

morintd avatar Apr 22 '24 19:04 morintd

may be related, getting this type error when I attempt to call .reset()

image

again, @morintd great work on this library, such a valuable tool <3

JoeRoddy avatar Jul 09 '24 15:07 JoeRoddy

Correct @JoeRoddy, the cause for that error you're seeing is the same.

electrovir avatar Jul 10 '24 12:07 electrovir