๐ 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
-
json
cast โ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)