fix(typescript): exclude null from Json type when column is NOT NULL#1070
Open
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
Open
fix(typescript): exclude null from Json type when column is NOT NULL#1070selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
Conversation
The Json type definition includes null in its union (Json = string | number | boolean | null | ...), so even when a jsonb column is NOT NULL, the generated TypeScript type allowed null values. For non-nullable jsonb/json columns, generate NonNullable<Json> instead of Json so that the TypeScript type accurately reflects the Postgres constraint. Fixes supabase#1055 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
JSONB NOT NULLcolumn allowsnull#1055:jsonb NOT NULLcolumns incorrectly generate a TypeScript type that allowsnullJsontype definition includesnullin its union (string | number | boolean | null | ...), so even non-nullable jsonb columns had null in their typegenerateNullableUnionTsType(), whenis_nullable=falseand the type isJson, returnNonNullable<Json>instead ofJsonChanges
src/server/templates/typescript.ts: Added a check ingenerateNullableUnionTsTypeto returnNonNullable<Json>for non-nullable Json columnstest/db/00-init.sql: Addednew_value jsonb NOT NULL DEFAULT '{}'::jsonbcolumn tousers_audittable to test the fixtest/server/typegen.ts: Updated all inline snapshots to reflectNonNullable<Json>for the new NOT NULL jsonb column (TypeScript, Go, Swift, Python)Behavior
Before this fix:
```typescript
// jsonb NOT NULL column → Json (which includes null!)
my_column: Json // but Json = string | number | boolean | null | ...
```
After this fix:
```typescript
// jsonb NOT NULL column → NonNullable
my_column: NonNullable // correctly excludes null
```
Nullable jsonb columns still correctly generate
Json | null.Test plan
new_value: NonNullable<Json>appears for the new NOT NULL jsonb column in the TypeScript snapshotprevious_value: Json | nullstill appears for the nullable jsonb column (no regression)new_value?: NonNullable<Json>(optional due to default value, but not null)npm test🤖 Generated with Claude Code