Skip to content

fix(misc): reduce any usage across packages - #2268

Merged
mandarini merged 3 commits into
developfrom
feat/reduce-any
Apr 29, 2026
Merged

mandarini merged 3 commits into
developfrom
feat/reduce-any

Conversation

@mandarini

@mandarini mandarini commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace ~50 explicit any annotations with unknown and proper type narrowing in internal code. Highest-value change: auth-js xform/fetch chain now takes unknown at the JSON boundary, preventing bugs like the signedUrl typo (#2254) from silently passing type checks.

  • catch blocks: e: any → e with instanceof narrowing
  • _getErrorMessage: any → unknown in auth-js and storage-js
  • xform functions: data: any → data: unknown (internal only)
  • websocket-factory: typed globalThis casts, renamed constructor field
  • auth-js eslint: enable no-explicit-any as warn

No breaking changes, all modifications are internal.

Why unknown remains in some places

Some unknowns here are deliberate. They fall into a few categories where tightening would either be incorrect or carry a real cost:

  • catch (e) — TypeScript enforces this. With useUnknownInCatchVariables (on under strict: true), catch (e: Error) is a compile error (TS1196). JavaScript allows throw <any value> — including strings, numbers, and null from third-party code we call into — so the catch binding genuinely cannot be narrower than unknown. Narrowing happens inside the block.

  • errors: unknown[] collected from third-party callbacks (e.g. _notifyAllSubscribers) — values come from user-supplied onAuthStateChange callbacks, which can throw anything. Downstream uses are only console.error(...) and throw, both of which accept any value. Tightening to Error[] would either lie about the type or require wrapping non-Errors, which loses fidelity for consumers throwing custom error classes.

  • Runtime-detection globals (WebSocketPair?: unknown, EdgeRuntime?: unknown in websocket-factory.ts) — used only for existence checks (typeof !== 'undefined'). EdgeRuntime in particular isn't standardized across edge runtimes (different shapes in Vercel Edge, Next.js Edge, polyfills). Typing it would be claiming knowledge that doesn't hold cross-runtime; unknown enforces existence-check-only access.

Why some anys are still in the call chain

  • JSON.parse and Response.json() return any — these are typed any in TypeScript's standard library, not by us. To eliminate any end-to-end here we'd need a runtime schema validator (zod / valibot / hand-rolled guards) at every response boundary. That's a non-trivial bundle/perf cost for an SDK shipped to bundle-sensitive runtimes (RN, edge, browsers), so the SDK leans on localized type assertions at the boundary instead.

  • User-extensible payloads — user_metadata, app_metadata, OAuth provider passthrough fields, PostgREST row shapes, Realtime broadcast payloads. The shape is defined by the consumer's application or by external services, not by Supabase. Where applicable these are typed via generics or Record<string, unknown>; the remaining [key: string]: any index signatures (e.g. on Session, User) preserve ergonomics for fields the server may add (token_type, provider_token, etc.) without forcing every caller to narrow.

What this PR does not do

  • Does not introduce runtime validation of wire data (zod or similar). Listed above as the path to remove the remaining any from JSON.parse/Response.json(), but out of scope here.
  • Does not tighten [key: string]: any index signatures on User/Session to unknown — would force narrowing on every read of provider passthrough fields. Deliberate trade-off, not an oversight.

@mandarini
mandarini requested review from a team as code owners April 22, 2026 14:07
@pkg-pr-new

pkg-pr-new Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2268

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2268

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2268

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2268

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2268

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2268

commit: 18c0c4e

@mandarini mandarini self-assigned this Apr 22, 2026
Comment thread packages/core/auth-js/src/lib/fetch.ts Resolved
Comment thread packages/core/auth-js/src/lib/fetch.ts Resolved
Comment thread packages/core/auth-js/src/lib/fetch.ts Resolved
Comment thread packages/core/auth-js/src/lib/fetch.ts Resolved
@mandarini
mandarini changed the base branch from master to develop April 28, 2026 10:17
@mandarini
mandarini requested a review from o-santi April 28, 2026 10:35
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.

2 participants