PHP code example of frolaxhq / laravel-typescript

1. Go to this page and download the library: Download frolaxhq/laravel-typescript library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

frolaxhq / laravel-typescript example snippets


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);
    }
}

'discovery' => [
    'auto_discover' => true,         // Automatically find models in your codebase
    'paths' => [app_path('Models')], // Add extra paths if needed
    'excluded_models' => ['BaseModel'],
],

'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' => [
    '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' => [
    'enabled' => true,
    'optional' => false,             // Make all relations optional
    'counts' => ['enabled' => true, 'optional' => true],
    'exists' => ['enabled' => true, 'optional' => true],
],

'mappings' => [
    'custom' => [
        'point' => '{ lat: number; lng: number }',
        'money' => 'string',
    ],
    'timestamps_as_date' => false,
],

'case' => [
    'columns' => 'camel',    // 'snake', 'camel', 'pascal'
    'relations' => 'camel',
],

'formatter' => [
    'enabled' => true,
    'tool' => 'prettier',    // 'prettier' or 'biome'
],

'cache' => [
    'enabled' => true,       // Skip unchanged models
],

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 }';
        }
    });
});

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',
        ],
    ];
}
bash
php artisan vendor:publish --tag=typescript-config
bash
php artisan typescript:generate
bash
php artisan typescript:generate --stdout
bash
php artisan typescript:generate User
bash
php artisan typescript:generate [model] [options]
bash
php artisan typescript:inspect "App\Models\User"
php artisan typescript:inspect "App\Models\User" --json
bash
php artisan typescript:mappings