Skip to content
HA_

← All writing

· 6 min read

A misspelt enum value that TypeScript let through

In Prisma 8's typed SQL builder, a where clause compared an enum column with any string, so a typo matched no rows and raised no error. The merged fix carries the enum type into the comparison and uses NoInfer so the typo cannot widen it.

open-sourcetypescriptprismatype-safety

Here is a query from Prisma 8's typed SQL builder. The kind column is an enum with two members, human and agent:

sql.public.actor.select('id').where((f, fns) => fns.eq(f.kind, 'humna'))

'humna' is a typo. Before the fix, this compiled. The query ran, matched no rows, and returned an empty list. No type error, no runtime error, nothing in the logs.

The odd part is that the same builder already caught this typo everywhere else. update({ kind: 'humna' }) failed to compile. So did insert([{ id: 'a1', kind: 'humna' }]), and so did the ORM client's where({ kind: 'humna' }). Only the comparison functions in a builder where let it through.

I fixed it in prisma/prisma#31019, merged on 18 November 2026, for issue #30492. This post covers why the type was lost, the first fix that looks right but is not, and the one TypeScript feature that made the real fix work.

Why a typo matters more in a where clause

A typo in an insert usually fails loudly. The database rejects it, or someone notices the bad row. A typo in a where fails quietly:

  • A read returns nothing, which looks like "no data yet".
  • An update changes nothing, and reports zero rows affected.
  • A delete deletes nothing, so the cleanup job you trusted never cleaned up.

Each of those looks like a normal result. That is why the type checker is the right place to catch it: there is no later stage that will.

Where the type got lost

The builder knows a lot about each column. It has the column's codec (here pg/text@1, Postgres text), whether it is nullable, and the type the contract resolves for it, which for an enum is the union 'human' | 'agent'. The select result already used that union. insert and update typed their values from it too.

But the where callback sees each column through a "scope", and the scope kept only two facts per column: the codec id and the nullability. The enum union was dropped at that point.

The comparison functions then typed a raw value from the codec:

eq: <CodecId extends string>(
  a: CodecExpression<CodecId, boolean, CT> | null,
  b: CodecExpression<CodecId, boolean, CT> | null,
) => Expression<BooleanCodecType>

CodecExpression accepts either another expression or a raw value of the codec's input type. For pg/text@1, the input type is string. Every string is a valid string, so 'humna' passed.

So the fix has two parts. First, keep the enum type on the column in the scope. Second, make the comparison use it.

Part one: carry the input type

The first part is plain plumbing. Each scope field got an optional, type-only input property, filled from the same per-column type that insert and update already used:

export type StorageColumnScopeField<
  Column extends { readonly codecId: string; readonly nullable: boolean },
  Input = never,
> = {
  codecId: Column['codecId'];
  nullable: Column['nullable'];
} & (Column extends { many: true } ? { many: true } : Record<never, never>) &
  ([Input] extends [never] ? Record<never, never> : { input?: Input });

"Type-only" means the property never exists at runtime. It only carries a type from the place that knows it (the contract) to the place that needs it (the comparison). Left-joined tables keep it too, as the union plus null.

Part two: the obvious fix, and why it fails

With input on the column, the comparison can infer it. The natural first try is a new type parameter, used on both sides:

declare function eq<C extends string, Input>(
  a: Expr<{ codecId: C; input?: Input }> | Input,
  b: Expr<{ codecId: C; input?: Input }> | Input,
): boolean
 
eq(kind, 'humna') // still compiles

It still compiles. I checked what TypeScript infers for Input here, and it is 'human' | 'agent' | 'humna'.

The reason is how inference works. TypeScript collects a candidate for Input from every place it appears. The column gives 'human' | 'agent'. The raw value 'humna' sits in a position typed as Input too, so it is also a candidate. TypeScript joins the candidates into a union, and the typo is now part of the type it was supposed to be checked against. The value checks itself.

This is a common trap in generic APIs. Any time a function infers a type from one argument and then checks another argument against it, the second argument can quietly widen the type.

NoInfer: "check against this, do not learn from it"

TypeScript 5.4 added a built-in for exactly this case: NoInfer<T>. It is the same type as T, but TypeScript will not use that position as a source of inference. The value there is only checked.

The fix types the raw operand as NoInfer<Input>:

export type ComparisonOperand<CodecId extends string, Nullable extends boolean, Input> =
  | Expression<{ codecId: CodecId; nullable: Nullable; many?: never; input?: Input }>
  | NoInfer<Input>
  | NullSuffix<Nullable>;

Now Input can only be learned from a column. The raw value has to fit what the column says. In a small repro with TypeScript 5.9:

error TS2345: Argument of type '"humna"' is not assignable to parameter of type
'"human" | "agent" | Expr<{ codecId: "pg/text@1"; input?: "human" | "agent" | undefined; }>'.

'human' passes. 'humna' fails with the same kind of error that update already gave.

Two cases needed a fallback. When neither operand is a column, such as a computed expression or a raw SQL fragment, there is nothing to infer from. So Input has a default, the codec's input type, and those comparisons behave as before:

type CodecInput<CodecId extends string, CT extends Record<string, { readonly input: unknown }>> =
  CodecId extends keyof CT ? CT[CodecId]['input'] : never;
 
eq: <CodecId extends string, Input = CodecInput<CodecId, CT>>(
  a: ComparisonOperand<CodecId, boolean, Input> | null,
  b: ComparisonOperand<CodecId, boolean, Input> | null,
) => Expression<BooleanCodecType>

The same operand typing went into ne, gt, gte, lt, lte, and the list forms of in and notIn.

Keeping an escape hatch

There is a real case for comparing with a value the enum no longer lists. Say a migration renames the member bot to agent. To find the rows that still hold bot, you have to compare with 'bot', which is no longer in the type. The issue asked to keep that possible.

A raw SQL operand keeps the codec's input type, so it still accepts any string:

fns.eq(fns.raw`kind`.returns('pg/text@1'), 'bot')

It is deliberate and visible in the code, which is what you want for the rare case. The everyday typo is now an error.

Testing types, not runtime

A runtime test cannot catch this bug. The query ran fine before the fix; it just matched nothing. The tests have to check what compiles and what does not.

So the PR added a type test file, enum-type.field-output.test-d.ts. It has six negative cases: eq with the column on either side, ne, a nullable enum, in and notIn. Each one fails on main and passes with the change. The positive cases cover in-union values, null on a nullable column, column-to-column comparisons and the raw SQL operand.

Checking that the negative tests fail on main first matters. A type test that "fails" for the wrong reason, such as a typo in the test itself, passes on both branches and proves nothing.

What it means for your code

The PR also added an upgrade note, because stricter types can break code that compiled before. A plain string is no longer accepted for an enum column:

function listByStatus(status: string) {
  // now a type error: string is wider than the enum
  return sql.public.account.select('id').where((f, fns) => fns.eq(f.status, status))
}

That error is useful. If status comes from a query string, nothing has checked it yet. The fix is to narrow it first:

const statuses = ['active', 'paused'] as const
type Status = (typeof statuses)[number]
const isStatus = (v: string): v is Status => (statuses as readonly string[]).includes(v)
 
function listByStatus(status: string) {
  if (!isStatus(status)) throw new BadRequestError(`unknown status: ${status}`)
  return sql.public.account.select('id').where((f, fns) => fns.eq(f.status, status))
}

Now an unknown status gets a clear 400, instead of an empty list that looks like "no results".

What I took from it

Two lessons, one narrow and one broad.

The narrow one: when a generic function checks one argument against a type learned from another, the checked argument should not take part in inference. Before TypeScript 5.4 people did this with tricks like T & {} or a second type parameter. Now NoInfer says it directly.

The broad one: if a library checks a value in three places and not in a fourth, the fourth is where the bugs will be. The types for the enum already existed. They just were not carried to the one place that needed them most, the where clause, where a wrong value fails without a sound.