Skip to content

fix(typescript): exclude null from Json type when column is NOT NULL#1070

Open
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
selenaalpha77-sketch:fix/jsonb-not-null-excludes-null
Open

fix(typescript): exclude null from Json type when column is NOT NULL#1070
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
selenaalpha77-sketch:fix/jsonb-not-null-excludes-null

Conversation

@selenaalpha77-sketch
Copy link
Copy Markdown

Summary

  • Fixes Generated type for JSONB NOT NULL column allows null #1055: jsonb NOT NULL columns incorrectly generate a TypeScript type that allows null
  • Root cause: The Json type definition includes null in its union (string | number | boolean | null | ...), so even non-nullable jsonb columns had null in their type
  • Fix: In generateNullableUnionTsType(), when is_nullable=false and the type is Json, return NonNullable<Json> instead of Json

Changes

  • src/server/templates/typescript.ts: Added a check in generateNullableUnionTsType to return NonNullable<Json> for non-nullable Json columns
  • test/db/00-init.sql: Added new_value jsonb NOT NULL DEFAULT '{}'::jsonb column to users_audit table to test the fix
  • test/server/typegen.ts: Updated all inline snapshots to reflect NonNullable<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

  • Verify that new_value: NonNullable<Json> appears for the new NOT NULL jsonb column in the TypeScript snapshot
  • Verify that previous_value: Json | null still appears for the nullable jsonb column (no regression)
  • Verify Insert/Update types show new_value?: NonNullable<Json> (optional due to default value, but not null)
  • Run full test suite: npm test

🤖 Generated with Claude Code

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>
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.

Generated type for JSONB NOT NULL column allows null

1 participant