spatie/typescript-transformer

Transformed 0 PHP types to TypeScript

Closed this issue · 1 comments

Diono commented

I'm running the script on windows 11 with PHP version 8.2.7 I've also tried several approaches without success:

set up a project with only "spatie/typescript-transformer" v2 installed... 0 TypeScript code generated.
create a Laravel project and publish the configuration of "spatie/laravel-typescript-transformer" v2... 0 TypeScript code generated.

+-----------+-------------------+
| PHP class | TypeScript entity |
+-----------+-------------------+
Transformed 0 PHP types to TypeScript

I've tested both approaches to commenting my PHP classes.

/** @typescript */

and

#[TypeScript]

I'm a little desperate

my composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": ["laravel", "framework"],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^10.10",
        "laravel/sanctum": "^3.2",
        "laravel/tinker": "^2.8",
        "spatie/laravel-typescript-transformer": "^2.3"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.18",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.1",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

my typescript-transformer.php

<?php
/*
 * Copyright (c) 2023 La Maison Fantastique - All Rights Reserved
 */

declare(strict_types=1);

use Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer;
use Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer;
use Spatie\TypeScriptTransformer\Collectors\DefaultCollector;
use Spatie\TypeScriptTransformer\Collectors\EnumCollector;
use Spatie\TypeScriptTransformer\Formatters\PrettierFormatter;
use Spatie\TypeScriptTransformer\Transformers\EnumTransformer;
use Spatie\TypeScriptTransformer\Transformers\InterfaceTransformer;
use Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer;

return [
    /*
     * The paths where typescript-transformer will look for PHP classes
     * to transform, this will be the `app` path by default.
     */

    'auto_discover_types'       => [app_path('/../../../www/app')],

    /*
     * Collectors will search for classes in the `auto_discover_types` paths and choose the correct
     * transformer to transform them. By default, we include a DefaultCollector which will search
     * for @typescript annotated and ![TypeScript] attributed classes to transform.
     */

    'collectors'                => [
        DefaultCollector::class,
        EnumCollector::class,
    ],

    /*
     * Transformers take PHP classes(e.g., enums) as an input and will output
     * a TypeScript representation of the PHP class.
     */

    'transformers'              => [
        DtoTransformer::class,
        EnumTransformer::class,
        InterfaceTransformer::class,
        SpatieEnumTransformer::class,
        SpatieStateTransformer::class,
    ],

    /*
     * In your classes, you sometimes have types that should always be replaced
     * by the same TypeScript representations. For example, you can replace a
     * Datetime always with a string. You define these replacements here.
     */

    'default_type_replacements' => [
        // DateTime::class               => 'string',
        // DateTimeImmutable::class      => 'string',
        // Carbon\CarbonImmutable::class => 'string',
        // Carbon\Carbon::class          => 'string',
    ],

    // The package will write the generated TypeScript to this file.
    'output_file'               => resource_path('/../../../www/src/@types/generated.d.ts'),

    /*
     * When the package is writing types to the output file, a writer is used to
     * determine the format. By default, this is the `TypeDefinitionWriter`.
     * But you can also use the `ModuleWriter` or implement your own.
     */

    'writer'                    => Spatie\TypeScriptTransformer\Writers\ModuleWriter::class,

    /*
     * The generated TypeScript file can be formatted. We ship a Prettier formatter
     * out of the box: `PrettierFormatter` but you can also implement your own one.
     * The generated TypeScript will not be formatted when no formatter was set.
     */

    'formatter'                 => PrettierFormatter::class,

    /*
     * Enums can be transformed into types or native TypeScript enums, by default
     * the package will transform them to types.
     */

    'transform_to_native_enums' => false,
];

I deliberately targeted a project outside the scope of Laravel because I don't use it in my project and I didn't want to pollute it with a Laravel installation.

puggan commented

are you sure you want app_path('/../../../www/app')
start a tinker and test that expression, to see if it returns what you expect it to return.

(also check resource_path('/../../../www/src/@types/generated.d.ts'))