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:
| Component | Extraction Method | Target Output |
|---|---|---|
| Eloquent Models | Evaluates model metadata, legacy/modern accessors, custom cast interfaces, and inspects database columns directly from the active connection. | interface or type |
| Relationships | Follows model declarations via BFS graph walk to resolve deep relation nesting, including polymorphic morph maps. | Optional object graphs |
| Form Requests | Statically analyzes rules() return structure to capture required, nested (*), nullable, and enum validations. | Request payload DTOs |
| API Resources | Uses static Abstract Syntax Tree (AST) analysis via nikic/php-parser on toArray() to capture conditional elements. | API response schemas |
| Routes | Inspects 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):
>= 16for Vite or Mix compilation.
1. Install the Package β
Install via Composer:
composer require hemilrajput/laravel-typegenNext, publish the configuration file:
php artisan vendor:publish --tag=typegen-configThis 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:
| Key | Default | Description |
|---|---|---|
scan_mode | 'attribute' | Can be 'attribute' (only scans classes marked with #[TypeScript]) or 'all' (scans every class in paths). |
paths | app_path('Models'), etc. | Directories containing target classes to scan. |
output.path | resources/js/types/generated.ts | The output path where generated types will be written. |
output.routes_path | resources/js/types/routes.ts | The output path for route parameter mappings. |
output.style | 'interface' | Type style: 'interface' or 'type'. |
output.zod | false | When true, compiles Zod validation schemas alongside TypeScript types. |
include_timestamps | true | Includes created_at and updated_at automatically. |
include_hidden | false | When false, excludes columns listed in your model's $hidden property. |
relations.wrap_with_relation | true | Wraps 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 β
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:
php artisan typescript:generate3. Generated Output β
By default, this will write a typescript definitions file to resources/js/types/generated.ts:
// 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;
}