Skip to content

fix(misc): widen enum-like unions with (string & {}) for forward compat - #2300

Merged
mandarini merged 1 commit into
developfrom
fix/widen-enum-unions
Apr 28, 2026
Merged

mandarini merged 1 commit into
developfrom
fix/widen-enum-unions

Conversation

@mandarini

@mandarini mandarini commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds | (string & {}) to closed string unions across all six packages so consumers can forward-compat against new server-supported values (or new client-side states) without losing literal-string autocomplete. Type-only change; no runtime behavior change.

The (string & {}) trick is used instead of plain | string because 'a' | string collapses to just string in TypeScript and loses the literal hints. The intersection prevents that collapse, so editors keep suggesting the known literals while the type also accepts arbitrary strings.

What changed?

Widened the following types:

  • auth-js (src/lib/types.ts, src/lib/webauthn.dom.ts):
    • AuthFlowType, MobileOtpType, EmailOtpType, AuthenticatorAssuranceLevels
    • JwtHeader.alg, JWK.kty
    • OAuthClientGrantType, OAuthClientType, OAuthClientRegistrationType, CustomProviderType
    • CredentialDeviceType, PublicKeyCredentialHint, AuthenticatorAttachment
    • getAlgorithm() parameter widened to match JwtHeader.alg (runtime already throws on unknown values via default:)
  • realtime-js:
    • RealtimeChannelSendResponse (src/RealtimeChannel.ts)
    • LogLevel, RealtimeRemoveChannelResponse, HeartbeatStatus (src/RealtimeClient.ts)
    • ConnectionState (src/lib/constants.ts)
  • storage-js (src/lib/types.ts): BucketType, VectorDataType, DistanceMetric
  • postgrest-js: count parameter (across select/insert/upsert/update/delete in PostgrestQueryBuilder.ts and rpc in PostgrestClient.ts), and the format parameter in PostgrestTransformBuilder.ts
  • functions-js (src/edge-runtime.d.ts): BeforeunloadReason
  • supabase-js (src/SupabaseClient.ts): removeChannel/removeAllChannels return types now reuse the (already widened) RealtimeRemoveChannelResponse from realtime-js instead of inlining the literal union

Skipped intentionally

  • WebAuthn challenge discriminator tags (type: 'create' | 'request') — these are discriminated-union tags used for narrowing the response shape, so widening them would defeat the purpose.
  • AggregateFunction ('count' | 'sum' | 'avg' | 'min' | 'max') — used as a token in template-literal type parsing of select queries; widening it would break the type-safe query parser.

Both are tracked as deliberate exclusions for future review.

Why was this change needed?

Server-decoded enums and forward-looking parameters should not be exhaustive in the SDK types. When the server adds a new value (a new OTP channel, a new heartbeat status, a new bucket type, a new count mode, etc.), consumers should be able to use it without waiting for an SDK release or resorting to as any. Keeping the literals as autocomplete hints preserves DX.

Screenshots/Examples

// Before: literal-only union
const status: RealtimeChannelSendResponse = await channel.send(...)
//    ^? 'ok' | 'timed out' | 'error'

// After: literals still autocomplete, but a new server-side status compiles too
const status: RealtimeChannelSendResponse = await channel.send(...)
//    ^? 'ok' | 'timed out' | 'error' | (string & {})
if (status === 'ok') { /* still narrows */ }

Breaking changes

  • This PR contains no breaking changes

Type-only widening at the runtime level, and patch-level safe in practice for TypeScript consumers. Two narrow theoretical edge cases:

  1. Exhaustive switch (x) { default: const _: never = x } over one of the widened unions will fail (the never no longer holds).
  2. Code that explicitly types a variable to the narrow form and assigns the widened return: const r: 'ok' | 'timed out' | 'error' = await ch.send(...) errors.

Both patterns are rare; standard usage (if (r === 'ok'), switches with a regular default branch, untyped consumption) is unaffected.

Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run npx nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

Additional notes

  • A separate follow-up PR will tackle making required properties optional on response types (Session, User, JwtHeader, JWK, RequiredClaims, Realtime payloads, PostgrestResponse*, FunctionsResponse*, FileMetadata, plus newer OAuth/MFA/Vector/SearchV2 shapes). That work is technically breaking for strict-mode TS consumers and is being kept out of this PR so the widening can land cleanly as a patch.
  • Verified locally: all six packages build, nx test:types postgrest-js passes (26/26).

@pkg-pr-new

pkg-pr-new Bot commented Apr 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

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

@supabase/functions-js

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

@supabase/postgrest-js

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

@supabase/realtime-js

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

@supabase/storage-js

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

@supabase/supabase-js

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

commit: decf6f9

@mandarini mandarini self-assigned this Apr 28, 2026
@mandarini
mandarini marked this pull request as ready for review April 28, 2026 11:37
@mandarini
mandarini requested review from a team as code owners April 28, 2026 11:37
@mandarini
mandarini marked this pull request as draft April 28, 2026 11:38
@mandarini
mandarini marked this pull request as ready for review April 28, 2026 11:38
@mandarini
mandarini merged commit 5bf83af into develop Apr 28, 2026
26 of 27 checks passed
@mandarini
mandarini deleted the fix/widen-enum-unions branch April 28, 2026 12:07
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