neofrog.
neofrog.
PPrisma
Created by neofrog. on 8/4/2024 in #help-and-questions
Prisma type inference is returning weird types
I'm encountering an issue with TypeScript not properly inferring the types when using PrismaClient in my Node.js typescript application. Setup: Prisma Schema:
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Account {
id String @id @default(uuid())
telegram_id BigInt @unique
telegram_username String? @unique
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Account {
id String @id @default(uuid())
telegram_id BigInt @unique
telegram_username String? @unique
}
Prisma Client Initialization:
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
export default prisma;
Issue:
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
export default prisma;
Issue:
When I perform a simple findUnique query, TypeScript infers the type as PrismaAccountClient<Account | null> instead of the expected model type.
const getAccount = async (telegram_id: number) => {
const account = await prisma.account.findUnique({
where: { telegram_id: telegram_id.toString() },
});

return account;
};

// Testing type inference
const testTypeInference = async () => {
const account = await getAccount(123456);

if (account) {
// TypeScript should infer that account is of type Account
// but instead it infers a complex Prisma type
console.log(account.telegram_username);
}
};

testTypeInference();
const getAccount = async (telegram_id: number) => {
const account = await prisma.account.findUnique({
where: { telegram_id: telegram_id.toString() },
});

return account;
};

// Testing type inference
const testTypeInference = async () => {
const account = await getAccount(123456);

if (account) {
// TypeScript should infer that account is of type Account
// but instead it infers a complex Prisma type
console.log(account.telegram_username);
}
};

testTypeInference();
Expected Type: account should be inferred as Account | null. Actual Type: account is inferred as Prisma
AccountClient<Account | null>. Question: How can I ensure that TypeScript infers the correct type (Account | null) for the result of the findUnique query without needing to manually set the types? Any help would be greatly appreciated!
19 replies