Model Typer

repository·master·Indexed 42 days ago

https://github.com/fumeapp/modeltyper

A developer tool that automates the generation of TypeScript interfaces and type aliases from Laravel Eloquent models. It synchronizes PHP backend models with TypeScript frontend types by analyzing database columns, mutators, and relationships. Supports Laravel >=v11.33.0 and PHP v8.2+, with a `model:typer` Artisan command for customizable type generation.

Tokens
3.9K
Snippets
16
Records
24
Agent score
52%

What's inside Model Typer

  1. Overview of Model Typer

    master
    Model Typer is a tool for Laravel and TypeScript developers that simplifies the generation of TypeScript interfaces from Laravel Eloquent models. It enhances type safety and consistency by ensuring your frontend types stay in sync with your backend model definitions.
  2. Requirements for Model Typer to work

    master

    For Model Typer to accurately detect relationships and mutators, you must provide explicit PHP return types for them in your Laravel models:

    1. Relationships: Must have a return type (e.g., HasMany, BelongsTo).
    2. Mutators: Must have a return type (e.g., Attribute).
    // Relationship requirement
    public function providers(): HasMany
    {
        return $this->hasMany(Provider::class);
    }
    
    // Mutator requirement
    protected function firstName(): Attribute
    {
        return Attribute::make(
            get: fn (string $value): string => ucfirst($value),
        );
    }
  3. Generate TypeScript types instead of interfaces

    master

    By default, Model Typer generates TypeScript interface definitions. To generate type aliases instead, use the --use-types flag via CLI, or set 'use-types' => true in your config/modeltyper.php file.

    # Via CLI
    php artisan model:typer --use-types
    
    # For a specific model
    php artisan model:typer --model=User --use-types
  4. Generate TypeScript interfaces with `model:typer`

    master
    Use the php artisan model:typer command to generate TypeScript interfaces based on your Laravel models' database columns, mutators, and relationships. You can pipe the output to a .d.ts file or use the output-file argument to save it directly.
    php artisan model:typer
  5. Verify environment requirements for Model Typer 3.0

    master

    When upgrading to version 3.0, ensure your environment meets the following minimum requirements:

    • Laravel: version 11.33.0 or higher
    • PHP: version 8.2 or higher
    • nesbot/carbon: version 3.5 or higher (required for PHP 8.4 support)
  6. Upgrade to Model Typer 3.0 from 2.x

    master
    Upgrading to version 3.0 involves several breaking changes regarding environment requirements, dependencies, and CLI options. Ensure your environment meets the new minimum requirements and update your configuration to include new options.
  7. Handle Eloquent Enums in TypeScript

    master

    Model Typer detects Laravel Enum casting. By default, it generates TypeScript Object Literals (which are safer and more performant). If you prefer TypeScript enum syntax, use the --use-enums flag.

    Note: Using --use-enums produces const enum by default, which are inlined and not readable at runtime. To ensure values are readable at runtime (e.g., via Object.values()), use the --plain-enums flag alongside --use-enums.

    # To get runtime-readable enums
    php artisan model:typer --use-enums --plain-enums
  8. Update Model Typer configuration for 3.0

    master

    Version 3.0 introduces new configuration options. To ensure your config/modeltyper.php file contains all necessary keys, delete your existing configuration file and republish the new one using the Artisan command below:

    php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=config
  9. Install Model Typer

    master

    Install Model Typer as a development dependency using Composer.

    Requirements:

    • Laravel >=v11.33.0 and PHP v8.2+
    • Note: For Laravel versions between 10.43.0 and 11.33.0, you must use version v2 of the package instead.

    After installation, you can optionally publish the configuration file to config/modeltyper.php to customize its behavior.

    composer require --dev fumeapp/modeltyper
    
    # Optional: Publish the config file
    php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=config
  10. Override or add type mappings in config

    master

    You can extend or override how Laravel types are mapped to TypeScript types by modifying the custom_mappings array in config/modeltyper.php. Use the Laravel type (or Custom Cast class) as the key.

    'custom_mappings' => [
        'App\Casts\YourCustomCast' => 'string | null',
        'binary' => 'Blob',
        'bool' => 'boolean',
        'point' => 'CustomPointInterface',
        'year' => 'string',
    ],
  11. Publish the Model Typer configuration

    master

    To customize the behavior of Model Typer, you can publish its configuration file to your application's config directory using the config tag. This allows you to control settings like whether to run automatically after migrations and where to save the output files.

    php artisan vendor:publish --tag=config
  12. Configure sum aggregates for relationships

    master

    To include sum aggregates (e.g., posts_sum_likes) in your TypeScript interfaces, define the $sums property in your Laravel model. The format is 'relationship_name' => 'column_to_sum'.

    protected $sums = [
        // Format: 'relationship' => 'column_to_sum'
        'posts' => 'likes',
    ];