Getting Started
Laravel TypeGen is a zero-dependency PHP package designed to turn your Eloquent models, Enums, and FormRequests into a single, clean TypeScript definitions file. It is built to keep your Laravel backend and Inertia frontend synchronized without manual type maintenance.
Requirements
- PHP: ^8.3
- Laravel: ^11.0, ^12.0, or ^13.0
Node.js (Optional but recommended)
Used if you compile your frontend with Vite or Mix.
- Node.js >= 16
1. Install the Package
Install via composer:
bash
composer require hemilrajput/laravel-typegenNext, publish the configuration file:
bash
php artisan vendor:publish --tag=typegen-configThis will create a configuration file at config/typegen.php.
Quick Start
To generate types for a model, simply annotate your class with 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. Generate the types
Run the generation command:
bash
php artisan typescript:generate3. 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;
}