Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow state and derived declarations inside class constructors #11455

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dummdidumm
Copy link
Member

This is a POC to test the feasability of allowing $state and $derived inside the constructor. Some cases still missing, and validation needs to adjusted to allow this.x = $state(0) inside the constructor but only at the top level of it.

Uncanney-valley-wise I think it's fine. TypeScript for example didn't have the ability to know that something was definitely initialized unless you either directly declared it or did so in the constructor (it did not follow method invocations inside the constructor) and it was honestly fine. I think the rule of only allowing it inside the constructor at the top level (i.e. not nested in an if block) is understandable and intuitive.

I'll proceed with this once we agreed on whether or not we want to do this.

closes #11116
closes #11339

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented May 3, 2024

🦋 Changeset detected

Latest commit: d45c8f0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

i'm in two minds. it definitely makes certain derived scenarios easier, but it does reduce clarity if people get into the habit of just doing this stuff in the constructor instead of declaring state fields. i also think it gets a little tricky around enumeration. it's one thing to say 'state fields are turned into accessors on the prototype, which are not enumerated on the instance'

(by which i mean this)

class Foo {
  a = 1;
  get b() { return 2 }
}

var foo = new Foo(); // { a: 1 }

but it's something else if you're doing something that very much looks like assigning to a property on the instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants