Lazy properties having types rendered as any
#442
-
Hello! I've been trying to migrate some of my existing API resources to this package to take advantage of typescript generation via <?php
namespace App\Data;
use App\Models\Chart;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Lazy;
/** @typescript */
class ChartOverviewData extends Data
{
public function __construct(
public string $id,
public string $name,
/** @var Lazy|\App\Data\ChartMetaData[] */
public Lazy|DataCollection $meta,
) {
}
public static function fromModel(Chart $chart): self
{
return self::from([...$chart->toArray(),
'meta' => Lazy::whenLoaded('meta', $chart, fn () => ChartMetaData::collection($chart->meta)),
]);
}
} Generated Type from ModuleWriter on export type ChartOverviewData = {
id: string;
name: string;
meta: any | Array<ChartMetaData>;
}; Has anyone else got experience of this, or is there something I am doing wrong? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Could you try replacing:
With:
I think that should fix the issue, I also want to remove support for these annotations in the future since they make things way to complicated, attributes FTW! |
Beta Was this translation helpful? Give feedback.
-
Replacing --- a/config/typescript-transformer.php
+++ b/config/typescript-transformer.php
@@ -1,5 +1,7 @@
<?php
+use Spatie\LaravelData\Support\TypeScriptTransformer\DataTypeScriptTransformer;
+
return [
/*
* The paths where typescript-transformer will look for PHP classes
@@ -30,7 +32,7 @@
Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
- Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer::class,
+ DataTypeScriptTransformer::class,
],
|
Beta Was this translation helpful? Give feedback.
Replacing
DtoTransformer
withDataTypeScriptTransformer
in thetypescript-transformer
config worked for me.