Skip to main content
Sonamu automatically generates TypeScript types that provide complete type safety based on Entity definitions. This document explains how each Entity field type is converted to TypeScript types.

Type Conversion Overview

Field Types

Entity Prop → TypeScript Type auto-conversion and type safety

Nullable Support

nullable: true → T | null optional null handling

Array Types

type[] → T[] array type auto-generation

Relation Types

BelongsToOne → number (FK) relation field type conversion

Basic Type Conversion

Shows how each Entity field type is converted to TypeScript types.

Numeric Types

bigInteger vs integer: integer is sufficient for IDs or counts, but use bigInteger for timestamps (milliseconds) or very large numbers.

String Types

The length attribute doesn’t affect TypeScript types, but is converted to .max(length) validation in Zod.

Boolean Types

Date Types

JSON Serialization: In API responses, Date is transmitted as an ISO string (string). Zod’s z.date() automatically converts the string to a Date object.

UUID Types

TypeScript doesn’t have a dedicated UUID type, so it’s represented as string. Format is validated with .uuid() in Zod validation.

Numeric Precision Types

Two types for handling PostgreSQL high-precision numbers.

number vs numeric

Choosing number vs numeric:Use number when:
  • General numeric calculations
  • Decimal precision is not critical
  • Scientific calculations, statistics
Use numeric when:
  • Financial calculations (money, prices, balances)
  • Exact decimals are required
  • Cryptocurrency amounts

Enum Types

Entity Enums are converted to TypeScript Union types.
Benefits:
  • Autocomplete support
  • Type safety guaranteed
  • Compile errors for invalid values

JSON Type

Used when storing complex object structures as JSON.
The json type references the Zod schema specified by id. This schema must be defined directly in {entity}.types.ts.

Virtual Type

Computed fields not stored in the database.
Virtual fields are computed in Model’s Enhancer:

Vector Type

Type for vector search (pgvector).
dimensions doesn’t affect TypeScript types, but is used in PostgreSQL schema and Zod validation.

Relation Types

Fields representing relationships between Entities.

BelongsToOne (Many-to-One)

Field name transformation: useruser_idIn Entity, you define as user, but the actual TypeScript type becomes user_id.

OneToOne (One-to-One)

HasMany / ManyToMany

HasMany and ManyToMany are not included in base types. When you define a Subset, those types are included in the Subset type.

Nullable Handling

nullable: true is converted to TypeScript Union type.
Array nullable: nullable: true for string[] means the array itself can be null.

Complete Type Mapping Table

Mapping table of all Entity Prop types to TypeScript types.

Practical Example

Type conversion example for an actual User Entity.

Next Steps

Generated Types

Types of generated types and their uses

Zod Validation

Runtime validation with Zod

E2E Type Safety

End-to-end type safety

Field Types

Complete Entity field type reference