fix(typescript): generate non-nullable json columns as NonNullable<Json>#1085
Open
Maliik-B wants to merge 1 commit into
Open
fix(typescript): generate non-nullable json columns as NonNullable<Json>#1085Maliik-B wants to merge 1 commit into
Maliik-B wants to merge 1 commit into
Conversation
The generated Json type includes null, so a NOT NULL json/jsonb column was typed as nullable. Narrow it to NonNullable<Json> in the nullability helper so the type reflects the database constraint. Nullable json columns are unchanged since Json already covers null. Closes supabase#1055
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
NOT NULLjson/jsonbcolumn generates the TypeScript typeJson, but the generatedJsontype is itself nullable:So the column type structurally permits
nulleven though Postgres forbids it. Studio reports the column as non-nullable, but the generated types disagree.Closes #1055.
Fix
generateNullableUnionTsTypealready special-casesunknown/any(which includenull) by not appending| null. This applies the same reasoning toJson: a non-nullablejson/jsonbcolumn is narrowed toNonNullable<Json>so the type reflects the database constraint.Scope
json/jsonbcolumns change (JsonbecomesNonNullable<Json>). Nullablejsoncolumns are untouched, sinceJsonalready coversnull.isNullable: true, so they are unaffected. Array types ((Json)[]) are unaffected.Tests
Added a regression test that spins up a throwaway schema with a
NOT NULL jsonbcolumn and a nullablejsonbcolumn, generates types scoped to it, and asserts the non-nullable column isNonNullable<Json>while the nullable one staysJson | null. It follows the inline-schema pattern from the existing included/excluded-schemas test, so it adds no fixture churn to the shared snapshots.Note on the type-generation extraction
I see #1084 and supabase/pg-toolbelt#302 are moving these templates into
@supabase/postgrest-typegen. This patch targetstypescript.tshere; happy to port the same one-line guard to the extracted package instead if that is the better home. @avallete