DEV Community

Arnaldo Tomo
Arnaldo Tomo

Posted on

# ๐Ÿš€ I Built a Library That Auto-Generates TypeScript Types from Laravel Models

๐Ÿš€ 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:

  1. I'd create a model in Laravel
  2. I'd need to manually recreate the corresponding TypeScript interface
  3. Every time the model changed, I had to remember to update the TypeScript
  4. 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
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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',
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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',
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Automatically generates:

export const createProductSchema = z.object({
  name: z.string().max(255),
  price: z.number().min(0),
});
Enter fullscreen mode Exit fullscreen mode

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
});
Enter fullscreen mode Exit fullscreen mode

4. Real-time File Watcher

# Watches for changes and regenerates automatically
php artisan schema:watch
Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ 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
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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'
});
Enter fullscreen mode Exit fullscreen mode

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
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ™ 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:


If you try the library, let me know your experience! Feedback is always welcome! ๐Ÿš€

laravel #typescript #opensource #mozambique #fullstack #developer #productivity

Top comments (0)