Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrismockClient type is incorrect #871

Open
electrovir opened this issue Apr 8, 2024 · 1 comment
Open

PrismockClient type is incorrect #871

electrovir opened this issue Apr 8, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@electrovir
Copy link

Problem

This type cast is incorrect:

} as unknown as typeof PrismaClient & PrismockData;

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;
@morintd
Copy link
Owner

morintd commented Apr 22, 2024

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 morintd added the enhancement New feature or request label Jun 14, 2024
@morintd morintd self-assigned this Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants