Skip to content

Getting Started ​

Laravel TypeGen is a zero-dependency PHP package designed to turn your Eloquent models, Enums, FormRequests, and API Resources into a single, clean, or split TypeScript definitions file. It is built to keep your Laravel backend and Inertia frontend synchronized perfectly, avoiding any manual type maintenance or drift.


Technical Design: How It Works ​

TypeGen uses a hybrid multi-layer inspection technique to resolve database schema, runtime PHP classes, and request validation states into TypeScript schemas:

ComponentExtraction MethodTarget Output
Eloquent ModelsEvaluates model metadata, legacy/modern accessors, custom cast interfaces, and inspects database columns directly from the active connection.interface or type
RelationshipsFollows model declarations via BFS graph walk to resolve deep relation nesting, including polymorphic morph maps.Optional object graphs
Form RequestsStatically analyzes rules() return structure to capture required, nested (*), nullable, and enum validations.Request payload DTOs
API ResourcesUses static Abstract Syntax Tree (AST) analysis via nikic/php-parser on toArray() to capture conditional elements.API response schemas
RoutesInspects Named Routes, reflection of controller signatures, and Eloquent bound model types.Strict route mappings

Requirements ​

  • PHP: ^8.3
  • Laravel: ^11.0, ^12.0, or ^13.0
  • Node.js (Optional): >= 16 for Vite or Mix compilation.

1. Install the Package ​

Install via Composer:

bash
composer require hemilrajput/laravel-typegen

Next, publish the configuration file:

bash
php artisan vendor:publish --tag=typegen-config

This will create a configuration file at config/typegen.php.


2. Configuration Reference ​

The published config/typegen.php configuration provides a simple but powerful customization layer:

KeyDefaultDescription
scan_mode'attribute'Can be 'attribute' (only scans classes marked with #[TypeScript]) or 'all' (scans every class in paths).
pathsapp_path('Models'), etc.Directories containing target classes to scan.
output.pathresources/js/types/generated.tsThe output path where generated types will be written.
output.routes_pathresources/js/types/routes.tsThe output path for route parameter mappings.
output.style'interface'Type style: 'interface' or 'type'.
output.zodfalseWhen true, compiles Zod validation schemas alongside TypeScript types.
include_timestampstrueIncludes created_at and updated_at automatically.
include_hiddenfalseWhen false, excludes columns listed in your model's $hidden property.
relations.wrap_with_relationtrueWraps relationship properties in optional helpers for clarity.

3. Quick Start ​

To generate types for a model, annotate the class using the #[TypeScript] attribute.

1. Annotate your model ​

php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hemilrajput\TypeGen\Attributes\TypeScript;

#[TypeScript]
class User extends Model
{
    protected $fillable = ['name', 'email'];
}

2. Run the generator ​

Run the Artisan generation command:

bash
php artisan typescript:generate

3. Generated Output ​

By default, this will write a typescript definitions file to resources/js/types/generated.ts:

typescript
// AUTO-GENERATED by laravel-typegen β€” do not edit by hand.

export interface User {
  id: number;
  name: string;
  email: string;
  created_at: string;
  updated_at: string;
}