Closed
Description
Strange DX when using nullable types in LiveAction.
Using the following LiveComponent:
#[AsLiveComponent('TestComponent', 'test_component.html.twig')]
class TestComponent
{
use DefaultActionTrait;
#[LiveAction]
public function action(#[LiveArg] ?int $id): void
{
//...
}
}
With the following template:
<div {{ attributes }}>
<a
data-action="live#action"
data-live-action-param="action"
data-live-id-param="{{ null }}"
>
Click me!
</a>
</div>
Expected:
The function is executed with $id === null
Current:
TypeError: "string" cannot be assigned to "int or "null".
Could we maybe include some sort of type transformation that when a LiveArg
is nullable, and the accepted types does not include string, and an empty string is passed in, the value is set to null?
Passing in the string "null"
to the param, it does coerce it to null
. Which just results in weird DX imo. Real world example:
<a
data-action="live#action"
data-live-action-param="setCreating"
data-live-parent-id-param="{{ parentId is not null ? parentId : 'null' }}"
>
Could we take a look at this behavior and perhaps tweak it so we don't need to cast null
to string "null"
when using nullable types on LiveAction
's LiveArg
s?
Metadata
Metadata
Assignees
Labels
No labels