fresh icon indicating copy to clipboard operation
fresh copied to clipboard

Wrong type (unknown) being inferred for context state

Open heyshubhamsinha opened this issue 4 years ago • 1 comments

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

Screenshot 2022-06-29 at 6 27 43 PM

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>`` ?

heyshubhamsinha avatar Jun 29 '22 13:06 heyshubhamsinha

TypeScript can not infer types across files. Explicitly specify the State type in the second type parameter slot for Handlers.

lucacasonato avatar Jun 29 '22 18:06 lucacasonato