Repository navigation
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
Conversation
…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
commit: |
This was referenced Nov 27, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #56.
A stack written inline as a host's
fetchtypesctxfrom the stack alone, as it is at runtime:MiddlewareandCompositeget a host form, listed ahead of the cascade:Outis read off the position's return type, the testpipeline'sSeedalready uses: a host'sfetchadmits a bareResponse.HostArgsis 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 themiddleware-conflictsentinel.withSupabasedepends on it before TypeScript 5.9 (the Risk in FUNC-1184).(req, ctx?: BaseContext) => Promise<Response>.Outis 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 declaredExportedHandler,Deno.serve, a collision inside a host export, and a middleware withInkeys 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 aDeno.serveshape against the built.d.tsat TypeScript 5.4.On #56's branch the same tests fail on every TypeScript version from 5.4.2 to 7.0.2:
On this branch they pass. The matrix below adds a 37-shape probe (Workers and
Deno.serveexports, nested, with config, composites, a nestedpipeline; handler positions undersatisfies 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-openfeatureand an internal dependent, typechecked against 1.0.0 and this branch through a tsconfigpathsoverride.tsconsrc, tests includedwithSupabasewithSupabase-shaped outers that fail on 1.0.0 toodeno checkon the@supabase/serverREADME files, #56's probe cases, and a host case using the realDeno.serveandDeno.ServeDefaultExport, with@supabase/server1.9.2 on the same copy:host.tsat 4 lines,feature.ts:14feature.ts:14host.tsat 4 lines,feature.ts:14feature.ts:14host.tsat 4 lines,feature.ts:14feature.ts:14host.tsat 4 lines,feature.ts:14feature.ts:14feature.ts:14is an unanchoredpipelinein 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 4where #56 printsof 3).Also run locally: lint, format,
vitest(151 passed), build, smoke,typecheck:min,typecheck:consumer, attw, the JSR dry run,test:denoandtest:bun.test:workerswasn't run: the built JavaScript differs from #56's only in a doc comment. On@supabase/serverwith 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
MiddlewareandCompositehave a fourth call signature. Calls are unaffected. A value given either type without coming fromdefineMiddleware,defineCompositeor a cast has to match it too.withSupabase,withOpenFeature, the authoring guide's variant) written inline as the export still typesctxwith the host's parameter. The guide says so and points to thepipelinearray.Inprerequisite written inline as the export still compiles when the host's env carries that key at that type, as on 1.0.0. Itsrunthen reads the key off an emptyctx.Promise<Response>counts as a host, the rulepipelinealready follows. A hand-written outer middleware whose handler parameter returnsResponse | Promise<Response>would start a nested middleware from the empty upstream. None of the dependents has one.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