Skip to main content
Learn about the type structure of services.generated.ts generated by Sonamu and how to use it.

Shared Types Overview

Single File

services.generated.ts All types in one place

Auto Generated

Extracted from backend No manual work needed

Type Reuse

Exported types Use throughout project

Consistency

Single source of truth No type conflicts

services.generated.ts Structure

File Overview

Sonamu generates all types and Services in a single file.
File size:
  • Usually 1,000 ~ 5,000 lines
  • Can be 10,000+ lines with many Entities and APIs
  • But all auto-generated so no management burden

Type Categories

1. Entity Types

Represents database table structure.
Characteristics:
  • Exactly same as backend Entity
  • All field types accurately mapped
  • null, undefined, union types all preserved

2. Subset Types

Defines subsets of Entity.
Subset naming convention:
  • A: Minimum fields (id + 1~2 core fields)
  • B: Medium fields (A + additional info)
  • C: All fields

3. API Parameter Types

Defines API function parameters.

4. API Response Types

Defines API function return types.
Types used in React Hooks.

Type Reuse

Using Type Helpers

Reuse existing types with TypeScript’s utility types.

Component Props

Use generated types as Props.

Form Data Types

Use API parameter types as form data.

State Management

Reuse types in global state as well.

Namespace Usage

Service Grouping

Services are grouped by Entity using Namespace.
Usage:
Benefits:
  • Prevents name collisions (getUser vs getPost)
  • Related functions logically grouped
  • Imports become concise
  • IDE auto-completion becomes more accurate

Type Import

Use type keyword when importing types only.
Tree-shaking:
  • import type is removed after compilation
  • Bundle size optimization
  • Build speed improvement

File Size Management

Large-scale Projects

Files can become very large with many Entities and APIs.
But it’s okay:
  1. βœ… Auto-generated: No manual management needed
  2. βœ… Tree-shaking: Unused code excluded from bundle
  3. βœ… IDE performance: Modern IDEs handle large files fine
  4. βœ… Type checking: TypeScript compiler processes efficiently

Code Splitting

You can split code with dynamic import if needed.

Version Control

Include in Git or Not

Include (recommended):
Benefits:
  • Ready to use immediately after pull
  • Can develop frontend without backend
  • Can track type change history
Don’t include:
Benefits:
  • Less conflicts
  • Each person generates latest version
  • Git history stays clean
Recommendation: Including is generally more convenient.

Resolving Conflicts

When merge conflict occurs:

Debugging

Check Generated Types

You can check types directly in IDE.
Shortcut:
  • VSCode: Cmd + Click (Mac) / Ctrl + Click (Windows)
  • Jump directly to type definition

Debugging Type Errors

When type error occurs:
  1. Check error message
  1. Check type definition
  1. Check backend
  1. Fix code

Cautions

Cautions when using shared types: 1. Don’t manually modify services.generated.ts 2. Use import type for bundle size optimization 3. pnpm generate required on API changes 4. Access Services through Namespace 5. Use Type Helpers when reusing types

Next Steps

API Type Inference

Understanding type inference

Compile-time Errors

Error detection methods

Using Services

Service usage

Custom Components

Writing components