Generate TypeScript constants and enums from Django models. No more copy-pasting values between backend and frontend.
You've got Django models with constants:
class Order(models.Model):
STATUS_PENDING = "pending"
STATUS_CONFIRMED = "confirmed"
STATUS_SHIPPED = "shipped"
STATUS_CHOICES = [
(STATUS_PENDING, "Pending"),
(STATUS_CONFIRMED, "Confirmed"),
(STATUS_SHIPPED, "Shipped"),
]
status = models.CharField(max_length=20, choices=STATUS_CHOICES)And you need the same values in TypeScript. You could copy them manually, forget to update one side, ship a bug, debug for an hour, then remember why you hate manual syncing.
Or just run:
python manage.py synctypesconstants.ts
export const Order = {
STATUS_CONFIRMED: "confirmed",
STATUS_PENDING: "pending",
STATUS_SHIPPED: "shipped",
} as constenums.ts
export enum OrderStatusEnum {
STATUS_PENDING = "pending", // Pending
STATUS_CONFIRMED = "confirmed", // Confirmed
STATUS_SHIPPED = "shipped", // Shipped
}index.ts
export * from "./constants";
export * from "./enums";- Python 3.10+
- Django 4.0+
pip install django-tsAdd to INSTALLED_APPS:
INSTALLED_APPS = [
# ...
"django_ts_constants",
]# Generate to ./frontend/constants (default)
python manage.py synctypes
# Custom output directory
python manage.py synctypes --output ./frontend/src/generated
# Check mode - exits 1 if files are outdated (for CI)
python manage.py synctypes --check
# Dry run - see what would change without writing
python manage.py synctypes --dry-runThe command scans your project's Django apps and extracts:
- Uppercase constants with underscores (
STATUS_PENDING = "pending")- Strings, integers, floats
- Choice tuples (
STATUS_CHOICES = [(value, label), ...])- Become TypeScript enums with labels as comments
Third-party packages are ignored. Only your project's apps are scanned.
# GitHub Actions
- name: Check TypeScript constants are in sync
run: python manage.py synctypes --checkIf someone updates a Django constant but forgets to regenerate the TypeScript files, CI fails. No more "works on my machine" surprises.
git clone https://github.com/regiscamimura/django-ts
cd django-ts
# Setup
uv venv && uv pip install -e ".[dev]"
# Test
pytest
# Lint
ruff check . && ruff format --check .MIT
Built by Regis Camimura