-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed as not planned
Labels
awaiting submitterneeds a reproduction, or clarificationneeds a reproduction, or clarification
Description
Describe the bug
using the $state rune on class properties hides them from Object enumeration.
Object.keys(), for in, spreading to clone do not work as I would expect.
<script>
class ToDo{
id = 0
name = $state(name)
constructor(name){
this.name = name;
}
}
Objects.keys(new ToDo("test")) // ["id"]
for(let prop in new ToDo("test")){
console.log(prop) // "id"
}
console.log({...(new ToDo("test"))}.name) // undefined
</script>Cause
The compiler changes the name field on the instance to private (#name) removing it from enumeration. The getters and setters are created attached to the class/prototype instead of the instance. The way they are set also hides them from direct enumeration on the prototype.
The behaviour of enumeration in JS in combination with prototype chains leads to the rune having a bigger effect than most developers would expect.
This could be fixed by injecting Object.defineProperties(...) into the constructor instead of getters and setters on the class.
Reproduction
Logs
No response
System Info
Tested in Chrome and Safari on macOSSeverity
blocking an upgrade
Metadata
Metadata
Assignees
Labels
awaiting submitterneeds a reproduction, or clarificationneeds a reproduction, or clarification