Skip to content

Allow type casting in @const declarations #15740

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

Closed
mnmr opened this issue Apr 11, 2025 · 4 comments
Closed

Allow type casting in @const declarations #15740

mnmr opened this issue Apr 11, 2025 · 4 comments

Comments

@mnmr
Copy link

mnmr commented Apr 11, 2025

Describe the problem

I'm iterating an array and need to render sub-elements, so would like to have a {@const x = item.node.data} to make accessing the data part easier. However, I need to cast this to a subtype, and Svelte 5 currently rejects all attempts to do this inline:
{@const x: ISub = item.node.data}
{@const x = <ISub>item.node.data}
{@const x = item.node.data as ISub}

I was able to work around the issue by declaring a cast function, but that is not a nice experience:
function cast(node: INode): ISub { return <ISub>node.data; } {@const x = cast(item.node)}

Describe the proposed solution

Support additional syntax in the @const parser logic to enable inline casting.

Importance

would make my life easier

@7nik
Copy link
Contributor

7nik commented Apr 12, 2025

It works, and in VSCode as well.
Do you have lang="ts" on <script> or maybe you use another IDE?

@paoloricciuti
Copy link
Member

I'll close this but feel free to reopen if it's an actual issue. Thanks for reporting!

@paoloricciuti paoloricciuti reopened this Apr 12, 2025
@paoloricciuti paoloricciuti closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2025
@mnmr
Copy link
Author

mnmr commented May 7, 2025

Sorry, I wasn't paying attention and missed the notifications. I'm using JetBrains Rider. It's absolutely an issue, though perhaps not in other IDEs.

@7nik
Copy link
Contributor

7nik commented May 7, 2025

JetBrains' Svelte plugin is known to still not have full Svelte 5 support.
I guess this issue is about this problem in the plugin.

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

No branches or pull requests

3 participants