fresh
fresh copied to clipboard
Wrong type (unknown) being inferred for context state
I'm trying to instantiate Supabase client using middleware. On doing so when I'm using ctx.state.supabase type being inferred is unknown.
// routes/_middleware.ts
import "https://deno.land/x/[email protected]/load.ts";
import {
createClient,
SupabaseClient,
} from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm";
import { MiddlewareHandlerContext } from "https://deno.land/x/[email protected]/server.ts";
interface State {
supabase: SupabaseClient;
}
export async function handler(
_req: Request,
ctx: MiddlewareHandlerContext<State>
) {
const SUPABASE_DB_URL = Deno.env.get("SUPABASE_DB_URL") ?? "";
const SUPABASE_ANON_KEY = Deno.env.get("SUPABASE_ANON_KEY") ?? "";
const supabase = createClient(SUPABASE_DB_URL, SUPABASE_ANON_KEY, "");
ctx.state.supabase = supabase;
const resp = await ctx.next();
return resp;
}
In routes/auth/signup.tsx
Upon checking the interface I found out Record<string, unknown> is causing the issue
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
extends ConnInfo {
next: () => Promise<Response>;
state: State;
}
Is there a specific reason for enforcing the input to be of type `Record<string, unknown>`` ?
TypeScript can not infer types across files. Explicitly specify the State type in the second type parameter slot for Handlers.