Skip to content

fix: type a stack written inline in a host-typed export without the host's values - #58

Draft
johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1184/middleware/host-typed-nesting
Draft

johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1184/middleware/host-typed-nesting

Conversation

@johnstonmatt

@johnstonmatt johnstonmatt commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #56.

A stack written inline as a host's fetch types ctx from the stack alone, as it is at runtime:

type Env = { DB: string; SECRET: string }

export default {
  fetch: withA(
    withDB(async (_req, ctx) => {
      ctx.a // string
      ctx.DB // withDB's contribution, no middleware-conflict with the binding
      ctx.SECRET // error: Property 'SECRET' does not exist
      return Response.json({})
    }),
  ),
} satisfies ExportedHandler<Env>

Deno.serve(withA(async (_req, ctx) => Response.json(ctx.a))) // no remoteAddr on ctx

Middleware and Composite get a host form, listed ahead of the cascade:

<Out = never>(
  ...args: HostArgs<Out, MiddlewareArgs<Config, NoConflict<Key, In & BaseContext,
    (req: Request, ctx: In & BaseContext & { [K in Key]: Contribution }) => Promise<Response>>>>
): HostProduced<In, Out>

type HostArgs<Out, Args> = [Out] extends [Promise<Response>] ? [never, never, never] : Args
  • Out is read off the position's return type, the test pipeline's Seed already uses: a host's fetch admits a bare Response.
  • Anywhere else HostArgs is a three-element tuple. TypeScript rechecks a generic rest parameter's arity once it is instantiated, so the form drops out before any argument is checked. It never contextually types a nested handler, and a TS2769 doesn't list it, so a collision still prints the middleware-conflict sentinel.
  • The cascade signature is unchanged: one type parameter, no default. A nested handler is typed from the cascade's return type only under that condition, and nesting inside a hand-written signature such as withSupabase depends on it before TypeScript 5.9 (the Risk in FUNC-1184).
  • In a host position the produced handler is (req, ctx?: BaseContext) => Promise<Response>. Out is removed from the return again, so it isn't widened.

Evidence

New tests, checked by tsc --noEmit:

  • src/core/define-middleware.test.ts, in a host-typed export: a Workers export (types and runtime), a key named like a binding, a declared ExportedHandler, Deno.serve, a collision inside a host export, and a middleware with In keys as the export.
  • src/core/define-composite.test.ts: a composite in a Workers export whose env has a key named like one of its parts.
  • test/ts-floor/consumer.ts: a Workers and a Deno.serve shape against the built .d.ts at TypeScript 5.4.

On #56's branch the same tests fail on every TypeScript version from 5.4.2 to 7.0.2:

src/core/define-composite.test.ts(352,9): error TS2769: No overload matches this call.
  Overload 1 of 3, '(config: { mode: "user" | "none"; }, handler: "middleware-conflict: key 'authMode' is already present on the upstream context") …
src/core/define-composite.test.ts(353,44): error TS2339: Property 'authMode' does not exist on type 'NoInfer<Record<never, never>> & { c: number; }'.
src/core/define-middleware.test.ts(1094,11): error TS2578: Unused '@ts-expect-error' directive.
src/core/define-middleware.test.ts(1113,16): error TS2769: No overload matches this call.
  Argument of type '[(_req: any, ctx: any) => Promise<Response>]' is not assignable to parameter of type '[handler: "middleware-conflict: key 'DB' is already present on the upstream context"] …
src/core/define-middleware.test.ts(1113,23): error TS7006: Parameter '_req' implicitly has an 'any' type.
src/core/define-middleware.test.ts(1113,29): error TS7006: Parameter 'ctx' implicitly has an 'any' type.
src/core/define-middleware.test.ts(1126,9): error TS2578: Unused '@ts-expect-error' directive.
src/core/define-middleware.test.ts(1139,11): error TS2578: Unused '@ts-expect-error' directive.

On this branch they pass. The matrix below adds a 37-shape probe (Workers and Deno.serve exports, nested, with config, composites, a nested pipeline; handler positions under satisfies FetchHandler, unanchored, typed, inside a composite and inside hand-written outers; collisions and unmet prerequisites in both). The dependents are @supabase/server (source and tests, examples, e2e), @supabase-labs/middleware-openfeature and an internal dependent, typechecked against 1.0.0 and this branch through a tsconfig paths override.

TypeScript tsc on src, tests included probe dependents vs 1.0.0 server's 3 tests nesting in withSupabase
5.4.2 pass pass, except 2 withSupabase-shaped outers that fail on 1.0.0 too +0 / −0 3 errors, the same 3 as on 1.0.0
5.5.4 pass pass +0 / −0 pass
5.6.2 pass pass +0 / −0 pass
5.6.3 pass pass +0 / −0 pass
5.7.3 pass pass +0 / −0 pass
5.8.3 pass pass +0 / −0 pass
5.9.2 pass pass +0 / −0 pass
5.9.3 pass pass +0 / −0 pass
6.0.3 pass pass +0 / −0 pass
7.0.2 pass pass +0 / −0 pass

deno check on the @supabase/server README files, #56's probe cases, and a host case using the real Deno.serve and Deno.ServeDefaultExport, with @supabase/server 1.9.2 on the same copy:

Deno (TypeScript) 1.0.0 this branch
2.1.4 (5.6.2) fails host.ts at 4 lines, feature.ts:14 feature.ts:14
2.4.5 (5.8.3) fails host.ts at 4 lines, feature.ts:14 feature.ts:14
2.5.0 (5.9.2) fails host.ts at 4 lines, feature.ts:14 feature.ts:14
2.9.8 (6.0.3) fails host.ts at 4 lines, feature.ts:14 feature.ts:14

feature.ts:14 is an unanchored pipeline in a handler position (FUNC-1177).

For collisions and unmet prerequisites in handler positions, tsc's output on 5.4.2, 5.6.2, 5.9.3 and 7.0.2 matches #56's apart from the overload count (Overload 1 of 4 where #56 prints of 3).

Also run locally: lint, format, vitest (151 passed), build, smoke, typecheck:min, typecheck:consumer, attw, the JSR dry run, test:deno and test:bun. test:workers wasn't run: the built JavaScript differs from #56's only in a doc comment. On @supabase/server with TypeScript 5.9.3, instantiations go from 259,566 to 262,341.

Merge Danger

Door: two-way

Types only: the built JavaScript is unchanged, and reverting restores #56's types.

Blast Radius: types

  • Middleware and Composite have a fourth call signature. Calls are unaffected. A value given either type without coming from defineMiddleware, defineComposite or a cast has to match it too.
  • A middleware with a hand-written signature (withSupabase, withOpenFeature, the authoring guide's variant) written inline as the export still types ctx with the host's parameter. The guide says so and points to the pipeline array.
  • A middleware with an In prerequisite written inline as the export still compiles when the host's env carries that key at that type, as on 1.0.0. Its run then reads the key off an empty ctx.
  • Any position whose return type admits more than Promise<Response> counts as a host, the rule pipeline already follows. A hand-written outer middleware whose handler parameter returns Response | Promise<Response> would start a nested middleware from the empty upstream. None of the dependents has one.
  • TS2769 messages count one more overload. A type test that matches the sentinel text, as the authoring guide's harness does, is unaffected.
  • Releases as a fix, in the same release as feat: type a pipeline in an anchored stack against the enclosing context #56 if they merge together.

Refs FUNC-1184

…ost's values

Written inline as a Workers ExportedHandler's fetch or as Deno.serve's handler, a nested stack took its upstream context from the host's second parameter: ctx offered the env's bindings, which are undefined at runtime, and a middleware whose key matched a binding failed with a false middleware-conflict.

Middleware and Composite get a host form, listed first. It reads the position's return type, the same test pipeline uses: a host's fetch admits a bare Response. There the handler's upstream is the empty context, as it is at runtime. Anywhere else its rest parameter is a three-element tuple, so TypeScript drops it on arity before checking any argument and leaves it out of the reported errors. The cascade signature keeps its single type parameter, so nesting inside a hand-written signature types as before on TypeScript 5.4 to 5.8.

Refs FUNC-1184
Written inline as a host's fetch, a middleware with a hand-written signature still types ctx with the host's second parameter. The authoring guide's hand-written variant says so, and points consumers to the pipeline array, which handles it.

Refs FUNC-1184
@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 22fb16e

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