Prisma Return Type
Hello, I have the following:
But I get an error on the "return".
Does someone know how to fix it?
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;
}
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;
2 Replies
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;
}
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