I’m encountering a TypeScript type inference issue in my TurboRepo setup, specifically when using Prisma methods within NestJS services. In the server package, I have an export.d.ts file that exports types for various controller methods and DTOs. However, TypeScript fails to infer complete types when using Prisma methods, resulting in incomplete type definitions when used in the frontend package.
Server Workspace:
// export.d.ts
import { AuthController } from './auth.controller';
import { SignInDto } from './auth.dto';
export * from './auth.controller';
// Partially exporting automatically typed return type
type Wrapper<T extends (...args: any) => any> = Awaited<ReturnType<T>>;
export namespace ApiTypes {
export interface SignInPayload extends SignInDto {}
export interface SignInResponse extends Wrapper<typeof AuthController.prototype.signin> { token: string }
}
// auth.service.ts
@Injectable()
export class AuthService {
constructor(private prisma: PrismaClient) {}
async signin(payload: SignInDto) {
// Using Prisma method
return await this.prisma.staffUsers.findUnique({
where: { username: payload.identifier },
});
}
}
In the auth.service.ts file, TypeScript fails to infer the complete export return type of the signin method when using this.prisma.staffUsers.findUnique. While TypeScript correctly infers types when returning objects directly from the AuthService, it struggles with types inferred from Prisma methods. It works as expected the Server Workspace but does not infer complte when used inside the frontend workspace.
Example
// Returning hard object works fine even outside the server package.
// The types automatically inferred as Promise<{name:string;age:number}>
async signin(payload: SignInDto) {
return { name: "Alice", age: 30 };
}
// But this does not work and infer type as Promise<any>
async signin(payload: SignInDto) {
return await this.prisma.staffUsers.findUnique({
where: { username: payload.identifier },
});
}
Note that the issue is only encountered when using the export.d.types outside the server package e.g. inside the react frontend package but works as expected in server package itself.
I would really apreciate any solutions and please feel free to ask for any further explaination or resources related to the issue.
I have already tried defining return types explicitely into the sign method but I cant do that here because types returned from prisma are too complex and may become cumbersome to maintain in many scenarios.