Prisma Types
Prisma generates a TypeScript client from your schema: a PrismaClient instance and types for each model, input, and query variant. The types match the schema at generate time, not at query time.
Detailed Explanation
You write schema.prisma. prisma generate writes a client into node_modules/.prisma/client (or a custom output). That client exports PrismaClient, model types (User), payload types that depend on select / include, and Prisma.UserWhereInput and friends. Your app imports those types.
The important typing behaviors:
findUnique returns User | null, not undefined. That is Prisma's convention and it matches SQL NULL vs no row (no row is null here). Map to undefined in a repository if the rest of the app uses undefined.
findMany returns User[]. select: { id: true } returns { id: string }[] — a narrower generated type, not User[]. Annotating the result as User[] is an error if you selected a subset, and a lie if you assert. Let inference work, or name the selected type with Prisma.UserGetPayload<{ select: { id: true } }>.
include adds relations. The return type grows. Prisma.UserGetPayload<{ include: { posts: true } }> is the explicit form when you need to name it.
Decimal, Bytes, and Json are special. Json is Prisma.JsonValue (string | number | boolean | { [key: string]: JsonValue } | JsonValue[] | null) — a recursive type. It is not your domain Settings. Parse Json at the boundary. Decimal is an object, not a number; do not do math as if it were.
Input types (UserCreateInput, UserUncheckedCreateInput) distinguish relation connect vs raw foreign keys. Using the wrong one is a type error, which is the point.
The client is a real object. PrismaClient is a class. You can inject it, or wrap it behind a repository so features do not import generated names. Tests use a fake repository, not a fake of the entire client (that mock is huge and loosely typed).
prisma generate must run in CI before tsc. If the schema changed and generate did not run, types are stale or the import fails. That is a pipeline issue, not a type-system one.
Prisma does not stop SQL injection in $queryRaw if you interpolate strings. Use $queryRaw tagged templates (see SQL injection). The typed client methods (findMany) are parameterized.
Code Examples
1. Inference on select — do not widen to User.
// 1. Inference on select — do not widen to User.
type User = { id: string; email: string; passwordHash: string };
function ids(rows: Array<{ id: string }>): string[] {
return rows.map((r) => r.id);
}
// Imagine prisma.user.findMany({ select: { id: true } }) returns { id: string }[]
const rows = [{ id: "u1" }];
console.log(ids(rows));2. GetPayload names an include variant.
// 2. GetPayload names an include variant.
type UserWithEmail = { id: string; email: string };
function publicUser(row: UserWithEmail): UserWithEmail {
return { id: row.id, email: row.email };
}3. findUnique is | null.
// 3. findUnique is | null.
async function emailOf(
find: (id: string) => Promise<{ email: string } | null>,
id: string,
): Promise<string | undefined> {
const row = await find(id);
return row?.email;
}Common Gotchas
Stale client after a schema change
run `prisma generate` before `tsc`. The types you import are files on disk.
`Json` is not your domain type
parse it. `Prisma.JsonValue` is a recursive union.
`findUnique` is `T | null`
not `undefined`. Convert in the repo if you need to match the rest of the app.
Asserting a selected row as the full `User` brings back unselected fields in the type only
they are `undefined` at runtime.