Skip to content

Allow let in reactive variable declarations #4965

@benmccann

Description

@benmccann

Is your feature request related to a problem? Please describe.
The TypeScript preprocessor does not yet fully support reactive statements, which makes it difficult to use TypeScript with Svelte.

There's an open question of how Svelte's syntax should be included in TypeScript. This is essentially a new syntax that needs to be spec'd out.

Describe the solution you'd like

It would be natural to do something like:

$: let squared: number = num * num;
$: let cubed: number = squared * num;

However, Svelte does not support let in reactive statements today.

Describe alternatives you've considered
Potentially we could find the first time a reactive variable used in the preprocessor and insert the let before passing it to TypeScript. However, this is sort of against the spirit of TypeScript, which expects some extra verbosity from the user in exchange for extra error checking such as checking whether you're referencing an undeclared variable.

I see no harm in allowing regular JS users to do:

$: let squared = num * num;
$: let cubed = squared * num;

How important is this feature to you?
TypeScript support in general is very important to myself and the community. Whether this is the best way to implement support or not is TBD.

Additional context

I think that the Svelte team should weigh in here on how we want this to work even if it's not this syntax. It doesn't seem right to me that the preprocessor would be in charge of deciding because then you could end up with a different syntax in each preprocessor and language-tools/VSCode might not know how to handle the syntax.

I believe that the preprocessor doesn't currently use the AST or do anything sophisticated and that the Svelte compiler is already tracking whether a variable usage is the first and should be a declaration or not. As a result, it would probably be easiest to implement in the Svelte compiler (for someone familiar with the Svelte compiler code. For someone new to the codebase that's probably flipped because the Svelte codebase is larger and more complicated in general.)

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