hemilrajput/laravel-typegen
| Install | |
|---|---|
composer require hemilrajput/laravel-typegen |
|
| Latest Version: | v2.2.3 |
| PHP: | ^8.2|^8.3|^8.4 |
| License: | MIT |
| Last Updated: | Aug 6, 2026 |
| Links: | GitHub · Packagist |
Laravel TypeGen
Laravel TypeGen — one artisan command turns your Eloquent models, Enums, and FormRequests into a single typed
.tsfile. No more hand-syncing PHP and TypeScript.
📖 Documentation
For complete documentation, guides, and setup instructions, visit laravel-typegen.github.io.
Why Laravel TypeGen?
- Keeps types in sync: Automatically reflect changes in your PHP models in your TypeScript interfaces. Generate TypeScript types from Eloquent models, Enums, and FormRequests. Built for the Laravel 13 + Inertia + React/Vue stack.
⚡️ The Killer Feature: Synchronized Types
Laravel TypeGen doesn't just generate standalone interfaces. It understands your application's logic.
PHP Enum + Model Cast:
enum UserRole: string {
case Admin = 'admin';
}
class User extends Model {
protected $casts = ['role' => UserRole::class];
}
TypeScript Output:
export type UserRole = 'admin';
export interface User {
id: number;
role: UserRole; // Automatically linked!
}
🚀 Features
- Eloquent Models: Generates interfaces from
$fillable,$casts, and timestamps. - Enums: Generates union types from backed and pure PHP enums.
- FormRequests & Zod: Generates request DTOs from your
rules()method and compiles them to Zod schemas for client-side validation. - API Resources: Transforms Laravel JsonResources into TypeScript representations.
- Routes & Ziggy: Generates strict types for named routes.
- VS Code Extension: Auto-generates types on file save.
- NPM Helpers: First-party
@hemilrajput/laravel-typegen-helperspackage forPaginatedResponse,InertiaForm, etc. - Attribute-Driven: Opt-in to generation using the
#[TypeScript]attribute. - Zero-Config: Smart defaults for standard Laravel projects.
📊 Comparison
| Feature | TypeGen | Spatie TS Transformer |
|---|---|---|
| Eloquent Support | ✅ | ✅ |
| Enum Support | ✅ | ✅ |
| FormRequest → DTO & Zod | ✅ | ❌ |
| API Resources | ✅ | ❌ |
| Relationship Auto-Discovery | ✅ | ❌ |
| Linked Enum Casts | ✅ | ⚠️ (Manual) |
| Attribute Driven | ✅ | ✅ |
| Inertia Native & NPM Helpers | ✅ | ⚠️ |
| VS Code Extension | ✅ | ❌ |
📦 Installation
composer require hemilrajput/laravel-typegen
🛠 Usage
1. Tag your classes
use Hemilrajput\TypeGen\Attributes\TypeScript;
#[TypeScript]
class User extends Model { ... }
2. Generate
php artisan typescript:generate
🔗 Relationships
Opt into relationship type generation per-model:
#[TypeScript(includeRelations: ['posts', 'profile'])]
class User extends Model { /* ... */ }
Related models (Post, Profile) are auto-discovered — no need to mark them separately. Generated output:
export interface User {
id: number;
posts?: Post[];
profile?: Profile | null;
}
export interface Post { /* ... */ }
export interface Profile { /* ... */ }
Relations are always emitted as optional (?) because they're only present when eager-loaded. This matches runtime reality.
Polymorphic relations
MorphTo is auto-supported when you register a morph map:
Relation::enforceMorphMap([
'post' => Post::class,
'video' => Video::class,
]);
Generates:
export interface Comment {
commentable?: (Post | Video) | null;
}
Without a morph map, emits unknown | null with a comment.
🗺 Roadmap
- Enum support (v0.2)
- FormRequest → DTO (v0.2)
- Eloquent relationships (v0.3)
- Route parameter types (v0.4)
- Watch mode (v0.4)
- Custom Cast class resolver (v0.4)
- VitePress documentation site (v1.0.0)
- Stable release (v1.0.0)
- API Resource generation (v1.3.0)
- VS Code Extension & NPM package (v2.0.0)
- Zod Schema Compilation (v2.2.0)
- Resource AST Parser for robust types
- Zod schema advanced constraints
Configuration
See config/typegen.php for all available options, including:
- Output path and style (interface vs type).
- Custom cast mapping.
- Prefix/suffix for generated names.
- Including/excluding timestamps and hidden fields.
License
MIT
Related Packages
Automatically generate TypeScript types and validation schemas from Laravel Mode...
PHP package for Laravel to type Eloquent models, routes, Spatie Settings with au...
End-to-end type-safe APIs for Laravel. Like tRPC, but for Laravel + TypeScript.
Simplified TypeScript types generator for Laravel applications with developer-de...
A Laravel package to export PHP Enums to TypeScript with labels, metadata, and t...