Skip to content

Conversation

@dev-hari-prasad
Copy link

@dev-hari-prasad dev-hari-prasad commented Dec 10, 2025

Description:
What changed?
Fixed the type inference for JSON path selections (->) by adding | null when the underlying JSON column can be null.

Why was this change needed?
When selecting a JSON path from a nullable JSON column, the generated TypeScript types incorrectly assumed the value could never be null.
In reality, the runtime returns null when the whole column is null, so the types were unsafe.
This patch brings the inferred type in line with actual behavior.

Closes/Fixes #1635

Screenshots/Examples:
N/A since it is a type-only change.

Breaking changes:
This PR contains no breaking changes.

📋 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:
The change is intentionally minimal and only affects the nullable branch of JsonPathToType.
It preserves existing behavior for non-nullable JSON columns while correctly reflecting actual return values for nullable ones.

CC @avallete & @mandarini

@dev-hari-prasad dev-hari-prasad requested review from a team as code owners December 10, 2025 17:27
@mandarini mandarini changed the title fix(postgrest-js): preserve nullability when using JSON path (->) (#1635) fix(postgrest): preserve nullability when using JSON path (->) (#1635) Dec 15, 2025
…upabase#1635)

The JSON path resolver (JsonPathToType) stripped `null` using Exclude<T, null>
but never added it back to the resulting type. This caused `.select('col->a')`
to infer a non-nullable type even when the underlying JSON column was nullable.

Example:
  json_col: { a: string } | null
  .select('json_col->a')   // inferred `string` instead of `string | null`

This patch re-attaches `| null` when the root JSON column contains null,
ensuring correct type inference for nullable JSON columns and nullable
nested paths.

Fixes: supabase#1635
@mandarini mandarini force-pushed the fix/Null-should-be-included-in-return-type branch from 60a5dcc to 621e051 Compare December 15, 2025 15:37
@mandarini mandarini added the postgrest-js Related to the postgrest-js library. label Dec 15, 2025
@mandarini
Copy link
Contributor

Thanks for the fix @dev-hari-prasad ! This correctly addresses the nullability issue in #1635. However, since this changes type inference in a way that could cause compilation errors for existing users (nullable JSON paths will now correctly return T | null instead of T), I will be treating this as a breaking change.

Before merging, could you please:

  1. Add type tests that verify the new nullable behavior
  2. Update any existing type tests that may be affected

I'll tag this for v3 (next major release). And we can merge it when the time comes!

@mandarini mandarini added v3 Planned for v3 do-not-merge Do not merge this PR. labels Dec 15, 2025
@mandarini mandarini self-assigned this Dec 15, 2025
@coveralls
Copy link

Coverage Status

coverage: 95.367% (+14.1%) from 81.261%
when pulling 621e051 on dev-hari-prasad:fix/Null-should-be-included-in-return-type
into 31280b2 on supabase:master.

@mandarini mandarini changed the title fix(postgrest): preserve nullability when using JSON path (->) (#1635) fix(postgrest): preserve nullability when using JSON path (#1635) Dec 15, 2025
@mandarini mandarini changed the title fix(postgrest): preserve nullability when using JSON path (#1635) fix(postgrest): preserve nullability when using JSON path Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge Do not merge this PR. postgrest-js Related to the postgrest-js library. v3 Planned for v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null should be included in return type when using json path (->)

3 participants