Skip to content

fix: check a handler with an optional ctx against a middleware with no prerequisites - #61

Draft
johnstonmatt wants to merge 1 commit into
mainfrom
FUNC-1200/middleware/empty-in-constraint
Draft

johnstonmatt wants to merge 1 commit into
mainfrom
FUNC-1200/middleware/empty-in-constraint

Conversation

@johnstonmatt

Copy link
Copy Markdown
Collaborator

Summary

A middleware or composite with no prerequisites now checks a pre-built handler whose ctx is optional:

const withA = defineMiddleware<'a', undefined, Record<never, never>, string>(…) // ctx.a is a string
const h = async (req: Request, ctx?: { a?: number }) => …                    // expects a number

withA(h) // main: compiles · here: No overload matches this call

Middleware and Composite constrained the upstream as In & BaseContext. With no prerequisites that is Record<never, never> & BaseContext, and TypeScript skips checking a handler against NoInfer<Base> & … when Base's constraint includes Record<never, never>, the handler's ctx is optional, and the key is optional. Writing the constraint as object or {} restores the check. Record<never, never>, any mapped type over no keys, and an empty interface don't.

 Middleware / Composite handler call forms
-  <Base extends In & BaseContext>
+  <Base extends BaseConstraint<In>>
+
+ type BaseConstraint<In> = keyof In extends never ? BaseContext : In & BaseContext

Authors keep writing Record<never, never>. BaseConstraint is internal and isn't exported from the package. The NoConflict TSDoc, which tells authors of hand-written signatures how to constrain Base, now says to use BaseContext when there are no prerequisites.

An optional ctx with optional keys is the shape of every produced handler with no prerequisites, so this is what a nested stack hands its outer layer. It is also how the ordering rule in #59 marks a key as out of order, so without this fix withGate(withOAuth(handler)) compiles when the stack isn't anchored.

Evidence

Check Result
Repo typecheck and tests identical errors and results with and without the change, on every branch
@supabase/server (source and tests, examples, e2e), @supabase-labs/middleware-openfeature, one internal dependent identical errors; their negative type tests still match
deno check on Deno 2.1.14 and 2.9.8 identical results
60 must-reject cases × 6 branches × 8 TypeScript versions (1,888 runs) none compiles with the change that is rejected without it; 112 runs are newly rejected, all of them the shapes above or #59's unanchored ordering cases
  • The two constraints are mutually assignable, accept and reject the same types, and have the same keyof. TypeScript doesn't treat them as identical, which is the behaviour this fixes.

Merge Danger

Door: two-way

Types only; the built JavaScript is unchanged, and reverting restores 1.0.0's types.

Blast Radius: types

Refs FUNC-1200

…o prerequisites

A middleware or composite with no prerequisites constrained its upstream as `Record<never, never> & BaseContext`. With that constraint TypeScript skips checking a pre-built handler whose optional `ctx` declares an optional key, so `withA(async (req, ctx?: { a?: number }) => …)` compiled even though `withA` contributes `a` as a string. The upstream is now constrained as `BaseContext` when there are no prerequisites. The two constraints accept the same types.

It also rejects an explicit upstream type argument that isn't an object, such as `withA<string>(handler)`.

Refs FUNC-1200
@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/middleware@61

commit: beedc7b

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant