🚀 I Built a Library That Auto-Generates TypeScript Types from Laravel Models
Hey dev community! 👋
I'm Arnaldo Tomo, a developer from Mozambique 🇲🇿, and I want to share with you a library I built to solve a problem that had been bugging me for ages in full-stack development.
😤 The Frustration That Drove Me
Like many of you, I frequently work with Laravel on the backend and React/Vue on the frontend. And I kept running into the same annoying situation:
- I'd create a model in Laravel
- I'd need to manually recreate the corresponding TypeScript interface
- Every time the model changed, I had to remember to update the TypeScript
- I'd inevitably forget, causing bugs in production
I kept thinking: "There has to be a better way to do this!"
💡 The Idea
After yet another afternoon lost debugging an error caused by outdated types, I decided: I'm going to automate this once and for all!
The idea was simple: analyze Laravel models and automatically generate the corresponding TypeScript types. But I wanted it to be:
- ✨ Zero configuration - Work immediately out of the box
- 🔄 Always synchronized - Never have outdated types again
- 🧠 Intelligent - Understand relationships, casts, validations
- ⚡ Fast - Perfect workflow integration
🛠️ Laravel AutoSchema Was Born
After several weeks of development (and many cups of coffee ☕), Laravel AutoSchema was born.
What the library does:
# Simple installation
composer require arnaldo-tomo/laravel-autoscema
# One command and you're done!
php artisan schema:generate
Example of what it can do:
My Laravel Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name', 'price', 'description'];
protected $casts = [
'price' => 'decimal:2',
'metadata' => 'json',
'status' => ProductStatus::class,
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews()
{
return $this->hasMany(Review::class);
}
}
Auto-generated TypeScript:
export interface Product {
id: number;
name: string;
price: number;
description: string;
metadata: Record<string, any> | null;
status: ProductStatus;
created_at: Date;
updated_at: Date;
// Relationships detected automatically!
category?: Category;
reviews?: Review[];
}
export enum ProductStatus {
ACTIVE = 'active',
INACTIVE = 'inactive',
DRAFT = 'draft',
}
🎯 Features I Developed
1. Intelligent Model Analysis
The library analyzes your models and detects:
- Fields and types automatically
- Eloquent relationships
- Custom casts
- PHP 8.1+ enums
2. Validation Schema Generation
From Form Requests:
class CreateProductRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
];
}
}
Automatically generates:
export const createProductSchema = z.object({
name: z.string().max(255),
price: z.number().min(0),
});
3. Typed API Client
When enabled, generates a complete API client:
// Super simple and typed usage!
const products = await productApi.getAll();
const product = await productApi.getById(1);
const newProduct = await productApi.create({
name: 'New Product',
price: 99.99
});
4. Real-time File Watcher
# Watches for changes and regenerates automatically
php artisan schema:watch
🌟 What I'm Most Proud Of
Zero Configuration
Works immediately after installation. I automatically detect:
- Model directories
- Relationships
- Data types
- Project structure
Full Compatibility
Extensively tested with:
- ✅ Laravel 9, 10, 11+
- ✅ Inertia.js
- ✅ React, Vue, Angular
- ✅ Different project structures
Performance
Optimized to be super fast. Even in projects with hundreds of models, generation is practically instantaneous.
📈 Numbers That Motivate Me
Since launch:
- 🚀 48 installations (and growing!)
- ⭐ Positive feedback from the community
- 🐛 Zero critical issues reported
- 🌍 Used in multiple countries
🔄 CI/CD Integration
One of the features I'm most proud of is the seamless CI/CD integration:
# GitHub Action I created
- name: Generate TypeScript types
run: php artisan schema:generate
- name: Commit if changes
run: |
git add resources/js/types/
git commit -m "Auto-update types" || exit 0
🎯 Real-World Use Cases
I'm seeing the library being used in:
E-commerce
// Typed and safe checkout
const order = await orderApi.create({
products: selectedProducts,
shipping_address: address,
payment_method: 'credit_card'
});
Dashboards
// Forms with automatic validation
const ProductForm: React.FC = () => {
const { register, handleSubmit } = useForm<CreateProductInput>({
resolver: zodResolver(createProductSchema) // Generated schema!
});
// The entire form is automatically typed
};
REST APIs
Developers using it to create typed SDKs for their Laravel APIs.
🚧 Challenges I Overcame
PHP AST Analysis
I had to dive deep into PHP code analysis to understand:
- Model structure
- Dynamic relationships
- Custom casts
Type Mapping
Creating perfect mapping between PHP/Laravel and TypeScript types was complex:
-
Carbon→Date -
jsoncast →Record<string, any> - Optional relationships →
Type | undefined
Performance with Large Projects
I optimized algorithms to work well even with:
- Hundreds of models
- Complex relationships
- Monolithic projects
🤝 Community Feedback
What motivates me most are messages like:
"Saved me hours of work!"
"Finally I can refactor without fear!"
"Perfect integration with Inertia.js!"
🔮 Next Steps
I'm working on:
v2.0 Features
- 🔧 GraphQL support - Generate types for GraphQL schemas
- 🎨 Customizable themes - Different output formats
- 📱 IDE plugins - VS Code integration
- 🌐 API Documentation - Generate docs automatically
Performance Improvements
- Smart caching
- Incremental generation
- Parallelization
📦 How to Try It
# Installation
composer require arnaldo-tomo/laravel-autoscema
# Initialization
php artisan schema:init
# Generation
php artisan schema:generate
# Watch mode for development
php artisan schema:watch
🙏 Asking for Community Support
As an indie creator from Mozambique, every:
- ⭐ GitHub star motivates me greatly
- 🐛 Reported issue helps me improve
- 💡 Suggestion inspires new features
- 🗣️ Share helps other developers
GitHub: https://github.com/arnaldo-tomo/laravel-autoscema
💭 Final Reflection
Developing Laravel AutoSchema taught me a lot about:
- Static code analysis
- Developer Experience (DX)
- The importance of automation
- How a simple tool can impact productivity
But what's most rewarding is knowing I'm solving a real problem that I and many other developers face daily.
🎉 Let's Connect!
I'm passionate about development and love talking about:
- Full-stack development
- Laravel ecosystem
- TypeScript
- Developer tools
- Open source
Find me at:
- 🐙 GitHub: @arnaldo-tomo
- 📧 Email: me@arnaldotomo.dev
- 🌍 Maputo, Mozambique 🇲🇿
If you try the library, let me know your experience! Feedback is always welcome! 🚀
Top comments (0)