Model Typer
repository·master·Indexed 42 days ago
https://github.com/fumeapp/modeltyperA 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.
What's inside Model Typer
- 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.
Requirements for Model Typer to work
masterFor Model Typer to accurately detect relationships and mutators, you must provide explicit PHP return types for them in your Laravel models:
- Relationships: Must have a return type (e.g.,
HasMany,BelongsTo). - 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), ); }- Relationships: Must have a return type (e.g.,
Generate TypeScript types instead of interfaces
masterBy default, Model Typer generates TypeScript
interfacedefinitions. To generatetypealiases instead, use the--use-typesflag via CLI, or set'use-types' => truein yourconfig/modeltyper.phpfile.# Via CLI php artisan model:typer --use-types # For a specific model php artisan model:typer --model=User --use-typesGenerate TypeScript interfaces with `model:typer`
masterUse thephp artisan model:typercommand to generate TypeScript interfaces based on your Laravel models' database columns, mutators, and relationships. You can pipe the output to a.d.tsfile or use theoutput-fileargument to save it directly.php artisan model:typerVerify environment requirements for Model Typer 3.0
masterWhen 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)
Upgrade to Model Typer 3.0 from 2.x
masterUpgrading 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.Handle Eloquent Enums in TypeScript
masterModel Typer detects Laravel Enum casting. By default, it generates TypeScript Object Literals (which are safer and more performant). If you prefer TypeScript
enumsyntax, use the--use-enumsflag.Note: Using
--use-enumsproducesconst enumby default, which are inlined and not readable at runtime. To ensure values are readable at runtime (e.g., viaObject.values()), use the--plain-enumsflag alongside--use-enums.# To get runtime-readable enums php artisan model:typer --use-enums --plain-enumsUpdate Model Typer configuration for 3.0
masterVersion 3.0 introduces new configuration options. To ensure your
config/modeltyper.phpfile 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=configInstall Model Typer
masterInstall 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
v2of the package instead.
After installation, you can optionally publish the configuration file to
config/modeltyper.phpto customize its behavior.composer require --dev fumeapp/modeltyper # Optional: Publish the config file php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=configOverride or add type mappings in config
masterYou can extend or override how Laravel types are mapped to TypeScript types by modifying the
custom_mappingsarray inconfig/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', ],Publish the Model Typer configuration
masterTo customize the behavior of Model Typer, you can publish its configuration file to your application's
configdirectory using theconfigtag. 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=configConfigure sum aggregates for relationships
masterTo include sum aggregates (e.g.,
posts_sum_likes) in your TypeScript interfaces, define the$sumsproperty in your Laravel model. The format is'relationship_name' => 'column_to_sum'.protected $sums = [ // Format: 'relationship' => 'column_to_sum' 'posts' => 'likes', ];