承接 hemilrajput/laravel-typegen 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hemilrajput/laravel-typegen

Composer 安装命令:

composer require hemilrajput/laravel-typegen

包简介

Generate TypeScript types from Eloquent models, Enums & FormRequests.

README 文档

README

Laravel TypeGen — one artisan command turns your Eloquent models, Enums, and FormRequests into a single typed .ts file. No more hand-syncing PHP and TypeScript.

📖 Documentation

For complete documentation, guides, and setup instructions, visit laravel-typegen.github.io.

Why Laravel TypeGen?

  • Keeps types in sync: Automatically reflect changes in your PHP models in your TypeScript interfaces. Generate TypeScript types from Eloquent models, Enums, and FormRequests. Built for the Laravel 13 + Inertia + React/Vue stack.

⚡️ The Killer Feature: Synchronized Types

Laravel TypeGen doesn't just generate standalone interfaces. It understands your application's logic.

PHP Enum + Model Cast:

enum UserRole: string {
    case Admin = 'admin';
}

class User extends Model {
    protected $casts = ['role' => UserRole::class];
}

TypeScript Output:

export type UserRole = 'admin';

export interface User {
    id: number;
    role: UserRole; // Automatically linked!
}

🚀 Features

  • Eloquent Models: Generates interfaces from $fillable, $casts, and timestamps.
  • Enums: Generates union types from backed and pure PHP enums.
  • FormRequests: Generates request DTOs from your rules() method.
  • Attribute-Driven: Opt-in to generation using the #[TypeScript] attribute.
  • Zero-Config: Smart defaults for standard Laravel projects.

📊 Comparison

Feature TypeGen Spatie TS Transformer
Eloquent Support
Enum Support
FormRequest → DTO
Relationship Auto-Discovery
Linked Enum Casts ⚠️ (Manual)
Attribute Driven
Inertia Native ⚠️

📦 Installation

composer require hemilrajput/laravel-typegen

🛠 Usage

1. Tag your classes

use Hemilrajput\TypeGen\Attributes\TypeScript;

#[TypeScript]
class User extends Model { ... }

2. Generate

php artisan typescript:generate

🔗 Relationships

Opt into relationship type generation per-model:

#[TypeScript(includeRelations: ['posts', 'profile'])]
class User extends Model { /* ... */ }

Related models (Post, Profile) are auto-discovered — no need to mark them separately. Generated output:

export interface User {
  id: number;
  posts?: Post[];
  profile?: Profile | null;
}

export interface Post { /* ... */ }
export interface Profile { /* ... */ }

Relations are always emitted as optional (?) because they're only present when eager-loaded. This matches runtime reality.

Polymorphic relations

MorphTo is auto-supported when you register a morph map:

Relation::enforceMorphMap([
    'post' => Post::class,
    'video' => Video::class,
]);

Generates:

export interface Comment {
  commentable?: (Post | Video) | null;
}

Without a morph map, emits unknown | null with a comment.

🗺 Roadmap

  • Enum support (v0.2)
  • FormRequest → DTO (v0.2)
  • Eloquent relationships (v0.3)
  • Route parameter types (v0.4)
  • Watch mode (v0.4)
  • Custom Cast class resolver (v0.4)
  • VitePress documentation site (v1.0.0)
  • Stable release (v1.0.0)

Configuration

See config/typegen.php for all available options, including:

  • Output path and style (interface vs type).
  • Custom cast mapping.
  • Prefix/suffix for generated names.
  • Including/excluding timestamps and hidden fields.

License

MIT

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固