Repository navigation
fix: check a handler with an optional ctx against a middleware with no prerequisites - #61
Draft
johnstonmatt wants to merge 1 commit into
Draft
johnstonmatt wants to merge 1 commit into
johnstonmatt wants to merge 1 commit into
Conversation
…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
commit: |
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
A middleware or composite with no prerequisites now checks a pre-built handler whose
ctxis optional:MiddlewareandCompositeconstrained the upstream asIn & BaseContext. With no prerequisites that isRecord<never, never> & BaseContext, and TypeScript skips checking a handler againstNoInfer<Base> & …whenBase's constraint includesRecord<never, never>, the handler'sctxis optional, and the key is optional. Writing the constraint asobjector{}restores the check.Record<never, never>, any mapped type over no keys, and an empty interface don't.Authors keep writing
Record<never, never>.BaseConstraintis internal and isn't exported from the package. TheNoConflictTSDoc, which tells authors of hand-written signatures how to constrainBase, now says to useBaseContextwhen there are no prerequisites.An optional
ctxwith 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 fixwithGate(withOAuth(handler))compiles when the stack isn't anchored.Evidence
test/ts-floor/consumer.tsagainst the built.d.tsat TypeScript 5.4. With the source change reverted, all four@ts-expect-errordirectives go unused (TS2578).typecheck:min,typecheck:consumer, lint, Prettier,test(127 passed),smoke,check-exports, the JSR dry run,test:denoand TypeDoc (same 4 warnings asmain) pass. The built JavaScript is byte-identical tomain.mainand to feat: type a pipeline in an unanchored stack against the enclosing context #55, feat: type a pipeline in an anchored stack against the enclosing context #56, fix: type a stack written inline in a host-typed export without the host's values #58, feat: let a middleware declare that it must run before another #59 and feat: check a config callback's ctx annotation as a prerequisite #60, on all eight TypeScript versions:@supabase/server(source and tests, examples, e2e),@supabase-labs/middleware-openfeature, one internal dependentdeno checkon Deno 2.1.14 and 2.9.8keyof. 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
ctxdeclares an optional key with a type that contradicts the enclosing contribution, and an explicit type argument that isn't an object (withA<string>(…)). The changelog should name both.objectwhere they showedRecord<never, never>as the upstream constraint. A type test that matches the exact text could notice; the authoring guide's harness matches the sentinel text, which is unchanged.fix).Refs FUNC-1200