frolaxhq/laravel-typescript
Composer 安装命令:
composer require frolaxhq/laravel-typescript
包简介
Generate TypeScript interfaces from your Laravel Eloquent models
README 文档
README
Generate TypeScript interfaces from your Eloquent models automatically. Keep your frontend types in sync with your database schema and model definitions.
📚 Read the Full Documentation
✨ Features
- Automatic Model Discovery — Automatically finds models in your codebase (PSR-4) without manual path config
- Automatic type generation — Scans your models and generates TypeScript interfaces/types
- Full type resolution — Precedence chain: overrides → docblocks → accessors → casts → DB types
- Import-aware overrides — Define external TypeScript symbols per field with automatic import dedup
- Relation support — Deep support for all Eloquent relations, counts, exists, and sums
- Enum support — Generate const objects, TypeScript enums, or union types
- Standalone Types — Define custom TypeScript interfaces in your config
- API Resources — Optional
{ data: T }response wrappers - Per-model files — One file per model with barrel export, or single bundled file
- Incremental builds — Intelligent caching for fast generation
- Formatter integration — Auto-format with Prettier or Biome
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
composer require frolax/laravel-typescript --dev
Publish the configuration:
php artisan vendor:publish --tag=typescript-config
Quick Start
Generate TypeScript definitions:
php artisan typescript:generate
Output to stdout:
php artisan typescript:generate --stdout
Generate for a specific model:
php artisan typescript:generate User
Output Example
Given this Eloquent model:
class User extends Model { protected $fillable = ['name', 'email']; protected $hidden = ['password']; protected $casts = [ 'email_verified_at' => 'datetime', 'role' => UserRole::class, ]; public function posts(): HasMany { return $this->hasMany(Post::class); } }
The generated TypeScript:
// This file is auto-generated by laravel-typescript. // Do not edit this file manually. export interface User { // Columns id: number; name: string; email: string; email_verified_at: string | null; role: UserRole; password: string; created_at: string | null; updated_at: string | null; // Relations posts: Post[]; // Counts posts_count?: number; } export const UserRole = { Admin: 'admin', User: 'user', } as const; export type UserRole = typeof UserRole[keyof typeof UserRole];
CLI Commands
typescript:generate
Generate TypeScript definitions from Eloquent models.
php artisan typescript:generate [model] [options]
| Option | Description |
|---|---|
model |
Generate for a specific model only |
--output=PATH |
Output path (overrides config) |
--writer=TYPE |
Writer: interface, type, or json |
--enum-style=STYLE |
Enum style: const_object, ts_enum, union |
--global |
Wrap in declare namespace |
--plurals |
Pluralize type names (User → Users) |
--fillables |
Generate fillable-only types |
--no-relations |
Exclude relations |
--no-counts |
Exclude relation counts |
--timestamps-as-date |
Map timestamps to Date instead of string |
--optional-nullables |
Make nullable columns optional (name?: type) |
--connection=NAME |
Database connection to use |
--strict |
Bail on first model error |
--stdout |
Output to stdout only |
typescript:inspect
Inspect a model's metadata:
php artisan typescript:inspect "App\Models\User" php artisan typescript:inspect "App\Models\User" --json
typescript:mappings
Show current type mappings:
php artisan typescript:mappings
Configuration
All options are documented in config/typescript.php. Key sections:
Discovery
'discovery' => [ 'auto_discover' => true, // Automatically find models in your codebase 'paths' => [app_path('Models')], // Add extra paths if needed 'excluded_models' => ['BaseModel'], ],
Output
'output' => [ 'path' => resource_path('types/generated'), 'per_model_files' => true, // One file per model 'barrel_export' => true, // Generate index.ts 'enum_directory' => 'enums', // Subdirectory for enums ],
Writer
'writer' => [ 'default' => 'interface', // 'interface', 'type', or 'json' 'enum_style' => 'const_object', // 'const_object', 'ts_enum', 'union' 'global_namespace' => null, // Wrap in declare namespace 'fillable_types' => false, // Generate UserFillable types ],
Relations
'relations' => [ 'enabled' => true, 'optional' => false, // Make all relations optional 'counts' => ['enabled' => true, 'optional' => true], 'exists' => ['enabled' => true, 'optional' => true], ],
Custom Type Mappings
'mappings' => [ 'custom' => [ 'point' => '{ lat: number; lng: number }', 'money' => 'string', ], 'timestamps_as_date' => false, ],
Naming Convention
'case' => [ 'columns' => 'camel', // 'snake', 'camel', 'pascal' 'relations' => 'camel', ],
Formatter
'formatter' => [ 'enabled' => true, 'tool' => 'prettier', // 'prettier' or 'biome' ],
Incremental Builds
'cache' => [ 'enabled' => true, // Skip unchanged models ],
Extension API
Use the Typescript facade to extend the package:
Custom Type Mappers
use Frolax\Typescript\Facades\Typescript; Typescript::extend(function ($registry) { $registry->registerMapper(new class implements TypeMapperContract { public function supports(string $type): bool { return $type === 'point'; } public function resolve(string $type): string { return '{ lat: number; lng: number }'; } }); });
Forced Type Overrides
Use the $interfaces property on your model:
class User extends Model { public array $interfaces = [ 'metadata' => 'Record<string, unknown>', 'settings' => 'UserSettings', 'attachments' => [ 'type' => 'MessagePartAttachment[]', 'import' => '@/types/ai', ], 'avatar' => [ 'type' => 'ImageAsset', 'nullable' => true, 'import' => '@/types/media', ], ]; }
String form and object form can be mixed in the same model.
Entries in $interfaces are emitted even when the field is not a database column,
accessor, or appended attribute. If the field already exists, the entry acts as a
forced type override.
When using import, generated files include import type statements automatically.
If multiple fields reference the same symbol/path pair, it is imported only once per output file.
Example generated import block:
import type { ImageAsset } from '@/types/media'; import type { MessagePartAttachment } from '@/types/ai';
Architecture
The package uses a pipeline architecture:
Discovery → Introspection → Metadata → Type Resolution → Relation Resolution → Writing → Formatting
Each stage has a clear contract and can be replaced or extended:
| Module | Contract | Default |
|---|---|---|
| Discovery | ModelDiscoveryContract |
ModelDiscovery |
| Introspection | SchemaIntrospectorContract |
LaravelSchemaIntrospector |
| Metadata | ModelMetadataExtractorContract |
ModelMetadataExtractor |
| Type Resolution | TypeResolverContract |
TypeResolver |
| Relations | RelationResolverContract |
RelationResolver |
| Writing | WriterContract |
TypescriptWriter |
| Formatting | FormatterContract |
NullFormatter |
Type Resolution Precedence
Types are resolved using an 8-level precedence chain:
- Forced override —
$interfacesproperty on model - API resource type — When
api_resourcesmode is enabled - Enum cast —
AsEnumCollection,AsEnumArrayObject - Accessor return type — PHP return type from accessor method
- Cast type — Eloquent
$castsproperty - DB column type — Raw database schema type
- Custom user mapping — From
config('typescript.mappings.custom') - Fallback —
unknown
Testing
composer test
The test suite includes 135+ tests covering:
- Unit tests for all components (mappers, resolvers, writers, cache)
- Integration tests (schema introspection, metadata extraction)
- E2E pipeline tests (full generation flow)
- Artisan command tests
License
MIT
frolaxhq/laravel-typescript 适用场景与选型建议
frolaxhq/laravel-typescript 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「interfaces」 「eloquent」 「types」 「typescript」 「codegen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 frolaxhq/laravel-typescript 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 frolaxhq/laravel-typescript 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 frolaxhq/laravel-typescript 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
O2System PSR it's build based on PHP Framework Interop Group (PHP-FIG) standards recommendations. This repository contains a collection of PHP interfaces based on the PSR-0 until the PSR-7.
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Limoncello PHP Framework Contracts (interfaces) required for components integration.
统计信息
- 总下载量: 72
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-01