Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaginatedDataCollection typescript conversion not working when part of union #981

Open
terley opened this issue Mar 21, 2025 · 0 comments
Open

Comments

@terley
Copy link

terley commented Mar 21, 2025

✏️ Describe the bug
I'm currently implementing Inertia 2.0. I'm using laravel-data (4.13.2) together with laravel-typescript-transformer (2.5.1). I have the following object:

<?php

namespace App\Data\App\Pages;

use App\Data\App\Asset\AssetData;
use App\Data\App\Asset\FilterAssetsData;
use Inertia\DeferProp;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\PaginatedDataCollection;

class ListAssetPage extends Data
{
    public function __construct(
        /** @var DeferProp | PaginatedDataCollection<AssetData> */
        public DeferProp | PaginatedDataCollection $assets,
    ) {}
}

Which is instantiated by calling this:

        return Inertia::render('VulnerabilityManagement/Assets/Index', new ListAssetPage(
            assets: Inertia::defer(fn () =>  AssetData::collect($query->paginate(
                perPage: $request->integer('per_page', 20),
                page: $request->integer('page', 1)
            ), PaginatedDataCollection::class)),
        ));

When I do the typescript conversion I dont get the paginated properties:

export type ListAssetPage = {
  assets: null | Array<App.Data.App.Asset.AssetData>;
};

When I remove the union, like so:

class ListAssetPage extends Data
{
    public function __construct(
        /** @var PaginatedDataCollection<AssetData> */
        public PaginatedDataCollection $assets,
    ) {}
}

I do get the paginated properties:

    export type ListAssetPage = {
        assets: {
            data: Array<App.Data.App.Asset.AssetData>;
            links: Array<{ url: string | null; label: string; active: boolean; }>;
            meta: {
                current_page: number;
                first_page_url: string;
                from: number | null;
                last_page: number;
                last_page_url: string;
                next_page_url: string | null;
                path: string;
                per_page: number;
                prev_page_url: string | null;
                to: number | null;
                total: number;
            };
        };
    };

↪️ To Reproduce
Provide us a pest test like this one which shows the problem:

See description above

✅ Expected behavior

I would expect, when part of the union that the typescript type would come out like this (just as without using the union):

    export type ListAssetPage = {
        assets: null | {
            data: Array<App.Data.App.Asset.AssetData>;
            links: Array<{ url: string | null; label: string; active: boolean; }>;
            meta: {
                current_page: number;
                first_page_url: string;
                from: number | null;
                last_page: number;
                last_page_url: string;
                next_page_url: string | null;
                path: string;
                per_page: number;
                prev_page_url: string | null;
                to: number | null;
                total: number;
            };
        };
    };

🖥️ Versions

Laravel: 11.44.2
Laravel Data: 4.14.1
PHP: 8.3.15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant