Prisma Return Type

Hello, I have the following:
async findOne<T extends Prisma.UserSelect>(
email: string,
select: T
): Promise<Prisma.UserGetPayload<{ select: T }>> {
const user = await this.prisma.user.findUnique({
where: { email: email },
select: { ...select }
});
return user;
}
async findOne<T extends Prisma.UserSelect>(
email: string,
select: T
): Promise<Prisma.UserGetPayload<{ select: T }>> {
const user = await this.prisma.user.findUnique({
where: { email: email },
select: { ...select }
});
return user;
}
But I get an error on the "return".
Type '(T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exte...' is not assignable to type 'T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exten...'.
Type 'null' is not assignable to type 'T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exten...'.

52 return user;
Type '(T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exte...' is not assignable to type 'T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exten...'.
Type 'null' is not assignable to type 'T extends undefined ? GetResult<{ id: string; created_at: Date; updated_at: Date; email: string; password: string; first_name: string; last_name: string; role: Role; }, unknown, never> & {} : { [K in keyof T as T[K] extends false | ... 1 more ... | undefined ? never : K]: T[K] extends object ? UserPayload<...> exten...'.

52 return user;
Does someone know how to fix it?
2 Replies
bakdaddy
bakdaddy16mo ago
Do you need an explicit type for return? you could just let ts infer return type
async findOne<T extends Prisma.UserSelect>(
email: string,
select: T
) {
const user = await this.prisma.user.findUnique({
where: { email: email },
select: { ...select }
});
return user;
}
async findOne<T extends Prisma.UserSelect>(
email: string,
select: T
) {
const user = await this.prisma.user.findUnique({
where: { email: email },
select: { ...select }
});
return user;
}
EQ
EQ16mo ago
But then it shows all the types findOne has instead of what I only selected so if I do findOne(email, { id: true }) it also shows for example the first, last name etc in intellisense/autocomplete
Want results from more Discord servers?
Add your server