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

Nullable Data object properties become not nullable (v2.5.0) #49

Open
ekateiva opened this issue Oct 10, 2024 · 3 comments
Open

Nullable Data object properties become not nullable (v2.5.0) #49

ekateiva opened this issue Oct 10, 2024 · 3 comments

Comments

@ekateiva
Copy link
Contributor

The update to v2.5.0 (using together with spatie/laravel-data ^3.12) is now causing issue with nullable properties.

Tha Data class:
Image

What php artisan typescript:transform brings:
Image

The typescript-transformer.php config that I am using:

<?php

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

    'auto_discover_types' => [
        // commented out because this is also included when
        // running the generate command with a custom --path setting
        // e.g.
        // `php artisan typescript:generate --path=nova-components/`
        // will also include the objects in app/ directory

        // app_path(),
    ],

    /*
     * 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' => [
        Spatie\LaravelData\Support\TypeScriptTransformer\DataTypeScriptCollector::class,
        Spatie\TypeScriptTransformer\Collectors\DefaultCollector::class,
        // Spatie\TypeScriptTransformer\Collectors\EnumCollector::class,
    ],

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

    'transformers' => [
        Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
        Spatie\LaravelData\Support\TypeScriptTransformer\DataTypeScriptTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\DtoTransformer::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\CarbonInterface::class => 'string',
        Carbon\CarbonImmutable::class => 'string',
        Carbon\Carbon::class => 'string',
        \Brick\Money\Money::class => \App\Data\Money\MoneyData::class,
    ],

    /*
     * The package will write the generated TypeScript to this file.
     */

    'output_file' => resource_path('js/generated.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' => null,

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

    'transform_to_native_enums' => true,

    /*
     * By default, this package will convert PHP nullable properties to TypeScript
     * types using a `null` type union. Setting `transform_null_to_optional` will
     * make them optional instead.
     */

    'transform_null_to_optional' => false,
];
@alexandre-tobia
Copy link

@rubenvanassche Exactly the same problem. Do you have any idea ? Thanks

@bram-pkg
Copy link
Contributor

bram-pkg commented Nov 6, 2024

We are also having this problem.

-        video: App.View.Inertia.Data.VideoData | null;
+        video: App.View.Inertia.Data.VideoData;

@mikinfo
Copy link

mikinfo commented Dec 3, 2024

Same here too.

Edit: It's spatie/typescript-transformer 2.3.1 > 2.4.0 upgrade that causes this breaking change. Seems related to the nullToOptional feature :
https://github.com/spatie/typescript-transformer/blob/main/CHANGELOG.md

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

4 participants