You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to instantiate private properties in my class, and to use some of them in the constructor. But unfortunately, the derived properties were null, even though they had all the necessary data to be valid.
This happens only with private derived properties. Non-private derived properties are initialized before the constructor and works fine. $state private properties are also working fine.
In this example, you will have console.log output from the constructor before one from derived.by
class Foo {
#state=$state('state')
#derived=$derived('derived from '+this.#state); // no matter what is inside, it will be null in constructor
#derivedBy=$derived.by(() => {
console.log('derivedBy');
return'derived from '+this.#derived// no matter what it returns, it will be null in constructor
});
initialconstructor() {
this.initial= [this.#state, this.#derived, this.#derivedBy]
console.log(this.initial) // ['state', null, null]
}
getderived() {returnthis.#derived}
getderivedBy() {returnthis.#derivedBy}
getstate() {returnthis.#state}
}
const foo = new Foo()
System Info
Just svelte 5.19.10, see repl.
Reproducible on Firefox 136 and Chrome 133, Safari 18.3
Severity
annoyance
The text was updated successfully, but these errors were encountered:
Yeah i think we should fix this...in the meantime if you want as a workaround you can pull that logic in a separate method from the constructor to make it work
classFoo{
#state =$state('state')
#derived =$derived('derived from '+this.#state);
#derivedBy =$derived.by(()=>{console.log('derivedBy');return'derived from '+this.#derived
});initialconstructor(){this.log();}log(){this.initial=[this.#state,this.#derived,this.#derivedBy]console.log(this.initial)}getderived(){returnthis.#derived}getderivedBy(){returnthis.#derivedBy}getstate(){returnthis.#state}}
Describe the bug
I was trying to instantiate private properties in my class, and to use some of them in the constructor. But unfortunately, the derived properties were
null
, even though they had all the necessary data to be valid.This happens only with private derived properties. Non-private derived properties are initialized before the constructor and works fine.
$state
private properties are also working fine.Reproduction
https://svelte.dev/playground/61fd75bbaa534daf8fbcef02580dbe9a?version=5.19.10
In this example, you will have
console.log
output from theconstructor
before one fromderived.by
System Info
Just svelte 5.19.10, see repl.
Reproducible on Firefox 136 and Chrome 133, Safari 18.3
Severity
annoyance
The text was updated successfully, but these errors were encountered: