Skip to content

push array is not proxied when array is optional #16685

@gotenxds

Description

@gotenxds

Describe the bug

Hi, I found an issue that might be expected behavior but it's odd as you can make it work by mutating unrelated state

When using the rune $state inside a class to expose a prop, myClass.prop it behaves as expected, it also behaves correctly when nesting it inside a sub property that is itself another class myClass.childClass.prop
But when that childClass is optional, that is myClass.childClass?.prop things get weird, primitives like string, boolean, numbers work fine, they are reactive all the way back to the ui, but when using an array method like push it is not reactive.

The odd thing about all of this is that if you have two or more $statess inside the class chain and modify any of them, the changes of the push do get propagated.

I created this simple repro in the svelte playground, it uses two classes,
TestClass and OptionalChild, the OptionalChild get initiated after one second using set Interval (but this works by any delayed initialization)

Inside we modify the array every second but it does not update in the UI, if you uncomment any of the lines that modify other props it will suddenly also update the array state in the UI

Reproduction

https://svelte.dev/playground/hello-world?version=5.38.6#H4sIAAAAAAAACn2STW_bMAyG_wohDLCNBU5yzUeHoaedeulOTQ-aQ89aZcmQmKRF4P8-Ss6HhaQ92IJIviRFPkdhZItiIbqdb4DQk5iIWmn0YvFyFPTRBWcwsP0U-rPrSr9HTcH2R3q8Z6-sITTEacTKV0519LAxG1JtZx3BEZ651KOW3kMPtbMtZOU0lD9lKP_5bBkEnMdTbAzWYPBwFeYF-zkifB7pF9dze6nzvID1Axyji1QNeUxrO1LWSP3YKL0tzu4N3TpL6VwZxpHPi-U57HxOp_DbVLZtDRJI8wG2BmrQI5CFVr4hKIKDdW8jwZ0Snhx8X0M2z65xMWxUe_D04egnMJ_NZmxaTS_DXG3Vng9gCc_zotZo_lIDfYgdIq6hT6cmBs1tXz9GGb5OEB5wNwE7EiWLwo-JIHwnsSC3w37yCVkpACldN74RYfgeqaoiT1ey4pLDU9fwzZMkzF9eh5UmTS8HjiJpbleRdfkFEAbrWbVod5RyxeA0yqePPwH6NLblZ4T6uE8eTVioSRpOBF80HWZ-MWZ8y4oAZp_O9pVvUumDMluxqKX22P8HmQvXjuUDAAA=

Logs

System Info

using firefox/chrome in svelte playground

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions