Download the PHP package fumeapp/modeltyper without Composer
On this page you can find all versions of the php package fumeapp/modeltyper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fumeapp/modeltyper
More information about fumeapp/modeltyper
Files in fumeapp/modeltyper
Package modeltyper
Short Description Generate TypeScript interfaces from Laravel Models
License MIT
Informations about the package modeltyper
Model Typer
Model Typer is a powerful tool designed for developers working with Laravel and TypeScript. Its primary purpose is to simplify the generation of TypeScript interfaces from Laravel models, enhancing type safety and consistency in your applications.
Why Model Typer over Laravel Wayfinder?
Laravel's first-party Wayfinder package is adding Eloquent model TypeScript
support on its dev-next branch. Model Typer has been doing this in production for years. A few things worth knowing
before switching:
-
It's already stable. Model Typer is versioned and running in production across a lot of apps. Wayfinder's model support is still in beta with an explicit note that "the API is subject (and likely) to change prior to the v1.0.0 release."
-
No dev dependencies. Getting Wayfinder's model support requires
composer require laravel/wayfinder:dev-next, which locks you to an untagged, moving branch instead of a proper release. -
Models are the whole point. Wayfinder is primarily a route and controller typing tool. Model support is being added on top of that. Model Typer was built specifically for this use case from the start.
- More output options. Model Typer generates columns, accessors, relationships, counts, exists, sums, and custom mappings with a lot of configuration options. Wayfinder's model output is still fairly basic by comparison.
Once Wayfinder hits a stable v1.0.0 with solid model support it will be worth a proper look. Until then, Model Typer is the practical choice.
Upgrade Guide
Please read the upgrade guide here
Installation
Starting support is for Laravel >=v11.33.0 and PHP v8.2+
[!IMPORTANT] For Laravel >=10.43.0 || <11.33.0, use v2 instead
Require this package with composer using the following command:
Optionally, you can publish the config file using the following command:
Usage
You can simply run the following command to generate TypeScript interfaces:
Or generate TypeScript types instead:
The output is an accurate, type-safe representation of Laravel models in TypeScript, such as:
How does it work?
This command will go through all of your models and make TypeScript Interfaces based on the database columns, mutators, and relationships.
You can then pipe the output into your preferred ???.d.ts, or set the optional argument
output-file to generate it
[!TIP] To view the current mappings that are being used, use the following command:
These mappings can be extended or overridden in the config
Interfaces vs Types
By default, Model Typer generates TypeScript interfaces, but you can also generate type aliases using the --use-types option or by setting use-types to true in the config file.
Interfaces (default)
Types (with --use-types)
When to use interfaces:
- When you need extensibility (other interfaces can extend them)
- For object-oriented patterns
- When you want declaration merging capabilities
When to use types:
- For more complex type definitions (unions, intersections, etc.)
- When you prefer a more functional approach
- For better performance in some TypeScript compilation scenarios
You can enable types in several ways:
Requirements
-
You must have a return type for your model relationships
- You must have a return type for your model mutations
Optional Arguments
- output-file : Echo the definitions into a file
Additional Options
- --model= : Generate typescript interfaces for a specific model
- --global : Generate typescript interfaces in a global namespace named models
- --json : Output the result as json
- --use-enums : Use typescript enums instead of object literals
- --plain-enums : Emit a plain enum instead of a const enum when --use-enums is set, so values are readable at runtime
- --use-types : Use typescript types instead of interfaces
- --plurals : Output model plurals
- --no-relations : Do not include relations
- --optional-relations : Make relations optional fields on the model type
- --no-hidden : Do not include hidden model attributes
- --no-counts : Do not include counts for relationships
- --optional-counts : Make relationship counts optional fields on the model type
- --no-exists : Do not include exists for relationships
- --optional-exists : Make relationship exists optional fields on the model type
- --no-sums : Do not include sums for relationships
- --optional-sums : Make relationship sums optional fields on the model type
- --timestamps-date : Output timestamps as a Date object type
- --optional-nullables : Output nullable attributes as optional fields
- --api-resources : Output api.MetApi interfaces
- --fillables : Output model fillables
- --fillable-suffix= : Appends to fillables
- --ignore-config : Ignore options set in config
Sum Aggregates for Relationships
Model Typer supports generating sum aggregates for Eloquent relationships. If a related model includes a summed value (e.g., the total number of likes on a user’s posts), this sum will be reflected in the generated output.
To enable this, define the sum relationship and target column using the $sums property on your model:
This will generate a corresponding field in the TypeScript interface:
This allows you to work with pre-calculated relationship totals directly in your frontend types.
Custom Interfaces
If you have custom interfaces you are using for your models you can specify them in a reserved interfaces array
For example for a custom Point interface in a Location model you can put this in the model
And it will generate:
This will override all columns, mutators and relationships
You can also specify an interface is nullable:
You can also choose to leave off the import and just use the type:
And it should generate:
Using the custom interface is also a good place to append any additional properties you want to add to the interface.
For example, if your interface keeps some additional state in something like Vuex, you can add it to the interfaces:
This will generate:
Override default mappings / add new ones
You can override the default mappings provided by Model Typer or add new ones by publishing the config
Then inside custom_mappings add the Laravel type as the key and assign the TypeScript type as its value
You can also add mappings for your Custom Casts
Declare global
Generate your interfaces in a global namespace named model
Output plural interfaces for collections
Exports for example, when a User model exists:
Output Api.MetApi* resources
Exports:
Enable all output options
Exports both plurals & api-resources. i.e. it is equivalent to:
For Single Model
Generate your interfaces for a single model
Output as JSON
Generate your interfaces as JSON
Enum Eloquent Attribute Casting
Laravel lets you cast Enums in your models. This will get detected and bring in your enum class with your comments:
[!NOTE] ModelTyper uses Object Literals by default instead of TS Enums for opinionated reasons. But you can use
--use-enumsoption to use TS Enums instead of Object Literals.With
--use-enums, ModelTyper emitsconst enumby default. Its members are inlined by the TypeScript compiler, so they aren't readable at runtime (e.g.Object.values(...)won't work). Add--plain-enumsalongside--use-enumsto emit a plainenuminstead, which produces a real object you can read from at runtime.
app/Enums/UserRoleEnum.php
Then inside our User model
app/Models/User.php
Now our ModelTyper output will look like the following:
[!NOTE] Notice how the comments are found and parsed - they must follow the specified format
All versions of modeltyper with dependencies
composer/class-map-generator Version ^1.6
illuminate/console Version ^11.33.0|^12.0|^13.0
illuminate/database Version ^11.33.0|^12.0|^13.0
illuminate/support Version ^11.33.0|^12.0|^13.0
