alexstewartja/laravel-typescript 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

alexstewartja/laravel-typescript

最新稳定版本:0.1.0

Composer 安装命令:

composer require alexstewartja/laravel-typescript

包简介

Generate TypeScript interfaces/definitions from Eloquent models

README 文档

README

Latest Stable Version Total Downloads GitHub Code Style Action Status License

PHP Versions Supported Laravel Versions Supported

Buy Me A Coffee

A Laravel package which allows you to quickly generate TypeScript interfaces/definitions for your Eloquent models.

Features

  • ✅ Database columns
  • ✅ Model relations
  • ✅ Model accessors
  • ⏳ Model casts
  • ⏳ Inherited relations (Traits/Mixins, etc.)

DBMS Compatibility (Laravel 11+)

  • ✅ pgsql (PostgresSQL)
  • ⏳ mysql (MySQL)
  • ⏳ mariadb (MariaDB)
  • ⏳ sqlsrv (Microsoft SQL Server)
  • ⏳ sqlite (SQLite)

Installation

You can install the package via composer:

composer require alexstewartja/laravel-typescript

Configuration

Publish the config file (config/laravel-typescript.php) with:

php artisan vendor:publish --provider="AlexStewartJa\TypeScript\TypeScriptServiceProvider" --tag="typescript-config"

Usage

Generate TypeScript interfaces for your Eloquent Models:

php artisan laravel-typescript:generate

Example

Eloquent Model

As an example, the following Product model is defined in an eCommerce app:

class Product extends Model
{
    protected $fillable = [
        'name',
        'price',
    ];
        
    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function features(): HasMany
    {
        return $this->hasMany(Feature::class);
    }
}

TypeScript Interface

Laravel TypeScript will generate the following TypeScript interface:

declare namespace App.Models {
    export interface Product {
        id: number;
        category_id: number;
        name: string;
        price: number;
        created_at: string | null;
        updated_at: string | null;
        category?: App.Models.Category | null;
        features?: Array<App.Models.Feature> | null;
    }
}

TS Interface Usage

This is an example usage with Vue 3:

import { defineComponent, PropType } from "vue";

export default defineComponent({
    props: {
        product: {
            type: Object as PropType<App.Models.Product>,
            required: true,
        },
    },
});

And another Vue 3 example for InertiaJS:

interface CartPageProps {
    products?: Array<App.Models.Product> | null;
    coupon_code?: string;
}

defineProps<CartPageProps>();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

A Lando file is included in the repo to get up and running quickly:

lando start

Please see CONTRIBUTING for more details.

Security

Please see SECURITY for more details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-02