Skip to content

Derived private property calculated after constructor #15288

Closed
@shadow-identity

Description

@shadow-identity

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 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
	});
	
	initial
	
	constructor() {
		this.initial = [this.#state, this.#derived, this.#derivedBy]
		console.log(this.initial) // ['state', null, null]
	}
	
	get derived() {return this.#derived}
	get derivedBy() {return this.#derivedBy}
	get state() {return this.#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions