kiwilan / typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. Optional NPM package for Inertia.
https://packagist.org/packages/kiwilan/typescriptable-laravel
MIT License
29 stars 2 forks source link

[Bug]: trait InteractsWithMedia produces invalid typescript #71

Closed mrrosenkilde2 closed 3 months ago

mrrosenkilde2 commented 6 months ago

What happened?

When a model uses the trait Spatie\MediaLibrary\InteractsWithMedia, the output looks like this:

media?: App.Models.$this->getMediaModel[]
media_count?: number

How to reproduce the bug

follow the installation instructions: https://github.com/kiwilan/typescriptable-laravel Install: https://github.com/spatie/laravel-medialibrary

runphp artisan make:model DemoModel Add the InteractsWithMedia trait, and add "use Spatie\Medialibrary\InteractsWithMedia", file should look like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\InteractsWithMedia;

class DemoModel extends Model
{
    use HasFactory, InteractsWithMedia;
}

run php artisan typescriptable

Generated output:

  export interface DemoModel {
    media?: App.Models.$this->getMediaModel[]
    media_count?: number
    id?: number
  }

Package Version

1.12.03

PHP Version

8.3.4

Which operating systems does with happen with?

Windows

Notes

There is currently a workaround by adding a file like so:

<?php

declare(strict_types=1);

namespace App\Concerns;

use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\MediaLibrary\InteractsWithMedia as BaseInteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

/**
 * Wrapper for "spatie/laravel-medialibrary" `InteractsWithMedia` trait
 * `kiwilan/typescriptable-laravel` unfortuneatly does not support dynamic lookup of the
 * model for the media relation, so we override the media relation with a hardcoded value.
 *
 * If the model is changed here, remember to change it in the media-library config file as well.
 *
 * @mixin \Spatie\MediaLibrary\HasMedia
 */
trait InteractsWithMedia
{
    use BaseInteractsWithMedia;

    public function media(): MorphMany
    {
        return $this->morphMany(Media::class, 'model');
    }
}

And then change use Spatie\MediaLibrary\InteractsWithMedia To use that file instead in your models.

php artisan about: Environment ...................................................................................................................................... Application Name ......................................................................................................................... Laravel Laravel Version .......................................................................................................................... 10.48.9 PHP Version ................................................................................................................................ 8.3.4 Composer Version ........................................................................................................................... 2.7.2 Environment ................................................................................................................................ local Debug Mode ............................................................................................................................... ENABLED URL .................................................................................................................................... localhost Maintenance Mode ............................................................................................................................. OFF

Spatie Permissions ...............................................................................................................................

Features Enabled ......................................................................................................................... Default Version .................................................................................................................................... 6.7.0

composer info spatie/laravel-medialibrar: name : spatie/laravel-medialibrary descrip. : Associate files with Eloquent models keywords : cms, conversion, downloads, images, laravel, laravel-medialibrary, media, spatie versions : * 11.4.7 released : 2024-04-24, this week type : library license : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText homepage : https://github.com/spatie/laravel-medialibrary source : [git] https://github.com/spatie/laravel-medialibrary.git 85f5f8284f827b8f2b426943e357e3df117ad1f5 dist : [zip] https://api.github.com/repos/spatie/laravel-medialibrary/zipball/85f5f8284f827b8f2b426943e357e3df117ad1f5 85f5f8284f827b8f2b426943e357e3df117ad1f5 path : C:\Users\Dennis\Herd\tid\vendor\spatie\laravel-medialibrary names : spatie/laravel-medialibrary

ewilan-riviere commented 4 months ago

I tried to reproduce your bug with these settings:

A repository is available here: https://github.com/ewilan-riviere/laravel-typescriptable-test/tree/issue-71

The output of php artisan typescriptable command is:

declare namespace App.Models {
  export interface ExampleMediaModel {
    media?: any[]
    media_count?: number
    id?: number
  }
}

Can you provide a repository with the bug?