Skip to content

[Bug]: generics not working on 3.0 #133

@hollow3464

Description

@hollow3464

What happened?

Generics are not properly generated for generic classes, the unkown type is used. This is the class I'm trying to transform, with valid class-level template. I'm using the laravel package.

This is my configuration form command.

        $config
            ->outputDirectory(__DIR__ . '/../../resources/js/types')
            ->formatter(PrettierFormatter::class)
            ->transformer(AttributedClassTransformer::class)
            ->transformer(EnumTransformer::class)
            ->replaceType(\DateTime::class, 'array{date: string, timezone_type: int, timezone: string}')
            ->replaceType(\DateTimeImmutable::class, 'array{date: string, timezone_type: int, timezone: string}')
            ->replaceType(\DateTimeInterface::class, 'array{date: string, timezone_type: int, timezone: string}')
            ->replaceType(\Illuminate\Http\UploadedFile::class, new TypeScriptIdentifier('File'))
            ->transformDirectories($appPath)
            ->extension(new LaravelTypeScriptTransformerExtension())
            ->writer(new GlobalNamespaceWriter('generated.d.ts'));

This is one of the classes I have

<?php

namespace App\Types;

use Spatie\TypeScriptTransformer\Attributes\TypeScript;

/**
 * @template T
 */
#[TypeScript]
final class PaginatedResponse
{
    public int $offset = 0;

    /**
     * @param  array<T>  $data
     */
    public function __construct(
        public int $page = 1,
        public int $pageSize = 25,
        public string $orderBy = 'id',
        public string $orderDirection = 'DESC',
        public array $data = [],
    ) {
        $this->offset = ($page - 1) * $pageSize;
    }
}

The resulting type is this, without the generic annotation and unkown array

        export type PaginatedResponse = {
            offset: number;
            page: number;
            pageSize: number;
            orderBy: string;
            orderDirection: string;
            data: unknown[];
        };

How to reproduce the bug

Just configuring the laravel package and launching the command

Package Version

3.0.0

PHP Version

8.4.14

Which operating systems does with happen with?

Linux

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions