Open adamjkb opened 1 year ago
@adamjkb so I did the following:
git clone https://github.com/prisma/prisma-client-extensions
cd prisma-client-extensions/static-methods
npm i
// created the .js file
And indeed I get any
But restarting the TS Server fixes the issue for me
Does that work for you too?
thanks for getting back to me @Jolg42. Indeed, on my end too, it gets that type however that is as far as they go. All client types are now gone. So, user
and all the other built-in types are gone. ($transaction
etc...).
And seemingly the type is the same across both versions but I believe it is hidden so it lies further up. So the difference is in the type of the $extends call gets back.
TS version:
(property) PrismaClient<Prisma.PrismaClientOptions, never, Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined, DefaultArgs>.$extends: ExtendsHook
<{
$allModels?: unknown;
user?: unknown;
password?: unknown;
}, unknown, {
user: unknown;
}, {
user: {
findSomething(arg: any): Promise<null>;
};
}, {
...;
}, {
...;
}, unknown, InternalArgs<...>, {
...;
}>(extension: ((client: DynamicClientExtensionThis<...>) => {
...;
}) | {
...;
}) => DynamicClientExtensionThis<...>
JS version:
property) PrismaClient<Prisma.PrismaClientOptions, never, Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined, DefaultArgs>.$extends: ExtendsHook
<{
$allModels?: unknown;
user?: unknown;
password?: unknown;
}, any, {
user: unknown;
}, {
user: {
findSomething(arg: any): Promise<null>;
};
}, {
$allModels?: unknown;
user?: unknown;
password?: unknown;
$allOperations?: unknown;
$executeRawUnsafe?: unknown;
$executeRaw?: unknown;
$queryRawUnsafe?: unknown;
$queryRaw?: unknown;
}, {
...;
}, any, InternalArgs<...>, {
...;
}>(extension: ((client: DynamicClientExtensionThis<...>) => {
...;
}) | {
...;
}) => DynamicClientExtensionThis<...>
It's hard to see and it is truncated but note the appearance of any
s on the line: }, any, InternalArgs<...>, {
and further up as well }, any, {
. These will essentially merge the client's functions to type any
if it were unknown
it were get ignored and others would take precedent but any
is "known" and overrides this.
I mentioned this on the PR but basically it has to do with how TS type server behaves in a JS environment: Every generics is initialized as any
:
The any fallback is one of the difference how JS is different from TS: https://github.com/microsoft/TypeScript/issues/30009#issuecomment-469385244
So, if we were to explicitly initialize these types to unknown
(and others to their default) this problem goes away.
You can actually hot patch my fixes from the PR in your node_modules file to see this behavior resolved by it since it doesn't affect any of the generated types.
I'm open to other solutions though, I can fully see my PR solving the problem at the wrong place and it's possible that this would be solved by further up the system.
Can confirm the bug still persists after version 5.0.0.
Reproducible with latest Prisma from main
branch.
Type of prisma
:
Type of prisma.user
:
Methods like prisma.$transaction
are also any
, and there are no useful autocompletions after typing prisma.
.
I reintroduce types using JSDoc as:
/** @type {import('@prisma/client').PrismaClient} */
const prisma = new PrismaClient().$extends({
// extension definition
})
But I lose typings on the extension, only getting the core client types.
Bug description
When using Prisma in a .js file types are erased once the Client is extended. This is happening whether the extension is mounted directly or created with
defineExtension
.How to reproduce
prisma
variableExpected behavior
Types to be inferred just as they would when Prisma is used in Typescript file (.ts)
Prisma information
Environment & setup
Prisma Version