evanschleret / laravel-typebridge
Generate deterministic TypeScript resources from Laravel resource attributes
Package info
github.com/EvanSchleret/laravel-typebridge
pkg:composer/evanschleret/laravel-typebridge
Requires
- php: ^8.2
- illuminate/console: ^12.0 || ^13.0
- illuminate/database: ^12.0 || ^13.0
- illuminate/filesystem: ^12.0 || ^13.0
- illuminate/http: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
Requires (Dev)
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.0 || ^4.0
- pestphp/pest-plugin-laravel: ^3.0 || ^4.0
README
Laravel TypeBridge
Deterministic TypeScript generation from Laravel resources.
Why this package
This package helps you keep backend resources and frontend types aligned, with deterministic output and predictable imports.
Requirements
- PHP
>=8.2 - Laravel
12.xor13.x - Note: Laravel
13.xrequires PHP>= 8.3(Laravel framework requirement)
Installation
composer require --dev evanschleret/laravel-typebridge
Publish the config:
php artisan vendor:publish --provider="EvanSchleret\\LaravelTypeBridge\\TypeBridgeServiceProvider" --tag=typebridge-config
Generated file:
config/typebridge.php
Basic usage
Generate files:
php artisan typebridge:generate
Use another output directory:
php artisan typebridge:generate --output-path=resources/typescript
Preview only (no write):
php artisan typebridge:generate --dry-run
Attribute example
Use TypeBridgeResource on Laravel resources (JsonResource or ResourceCollection):
<?php declare(strict_types=1); namespace App\Http\Resources; use App\Models\User; use EvanSchleret\LaravelTypeBridge\Attributes\TypeBridgeResource; use Illuminate\Http\Resources\Json\JsonResource; #[TypeBridgeResource( name: 'UserItem', structure: [ 'id' => 'number', 'email' => 'string|null', 'roles' => '@relation(roles)', 'manager?' => '@relation(manager)', ], )] final class UserResource extends JsonResource { public static string $model = User::class; }
Optional attribute fields:
types: local TypeScript aliases/enums declared in the same generated filefileName: per-resource output filename overrideappend: per-resource lines appended at the end of the generated filealiasBaseandaliasPlural: alias placeholders used bygeneration.append_templates
@relation(name) is strict:
- the resource model must be resolvable
- the relation method must exist
- the relation method must return an Eloquent relation
If the relation exists but no generated TypeScript type is available for the related model, the field falls back to any or any[].
Configuration
Published default config
This is the default config generated by vendor:publish:
<?php declare(strict_types=1); return [ 'output' => [ 'base_path' => resource_path('typescript'), ], 'sources' => [ app_path('Http/Resources'), ], 'generation' => [ 'use_semicolons' => false, 'generate_index' => true, 'shared_file' => '_api', 'shared_append' => [], 'append_templates' => [], ], 'files' => [ 'extension' => 'ts', 'naming_pattern' => '{name}', ], ];
Advanced config example (API wrappers + aliases)
This is an example, not the default:
'generation' => [ 'shared_file' => '_api', 'shared_append' => [ 'export interface ApiItemResponse<T> {', " status: 'success' | 'error'", ' data: T', '}', 'export interface ApiCollectionResponse<T> {', ' data: T[]', '}', ], 'append_templates' => [ [ 'name_ends_with' => 'Item', 'lines' => [ 'export type {base} = ApiItemResponse<{name}>', 'export type {basePlural} = ApiCollectionResponse<{name}>', ], ], ], ],
With RoleItem, this can generate:
export type Role = ApiItemResponse<RoleItem> export type Roles = ApiCollectionResponse<RoleItem>
Naming placeholders
For files.naming_pattern:
{name}{pascal}{camel}{snake}{kebab}
Example:
'files' => [ 'naming_pattern' => '{kebab}.types', ],
Template placeholders
Inside append_templates.*.lines:
{name}: resource name{base}: alias base (aliasBaseor suffix stripping){basePlural}: alias plural (aliasPluralor pluralized base){pascal}{camel}{snake}{kebab}
Override rules
fileNameon the attribute overridesfiles.naming_patternfor one resource--output-pathoverridesoutput.base_path
Other packages
If you want to explore more of my Laravel packages:
Open source
- Contributing guide: CONTRIBUTING.md
- Security policy: SECURITY.md
- Code of conduct: CODE_OF_CONDUCT.md
- License: LICENSE
