Replies: 2 comments 11 replies
-
I'm experiencing this as well and I totally agree with @ragulka that Spatie\LaravelData\Optional should be transformed into ?property instead of any. Is there another solution for this? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Are you using the correct transformer? This test: it('it can work with optional values', function () {
$config = TypeScriptTransformerConfig::create();
$data = new class () extends Data {
public string|Optional $name;
public string|null|Optional $description;
};
$transformer = new DataTypeScriptTransformer($config);
$reflection = new ReflectionClass($data);
dd($transformer->transform($reflection, 'DataObject')->transformed);
}); outputs: {
name?: string;
description?: string | null;
} |
Beta Was this translation helpful? Give feedback.
11 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have following Data object, where
$description
must either be astring
,null
(so that it's possible to unset it), orOptional
(so that it's possible toPATCH
update a product by not providing the description at all).When using typescript transformer, I get the following result. Note that the
any
type here is especially confusing.This is totally unexpected. I would have thought I get a result like this:
I tried adding the
Spatie\TypeScriptTransformer\Attributes\Optional
attribute, like this:And the result was following. Notice that the properties are now optional, but still have the
any
type.If I remove
Spatie\LaravelData\Optional
type:I finally get the correct typescript version:
However, now the properties are no longer truly optional when creating a new ProductData object:
I'm trying to understand if
Spatie\LaravelData\Optional
andSpatie\TypeScriptTransformer\Attributes\Optional
are supposed to achieve the same effect, or not? How can I achieve a result where a property is truly optional in both PHP and TypeScript, without getting theany
type?Beta Was this translation helpful? Give feedback.
All reactions