Skip to content

Allow type casting in @const declarations #15740

Closed as not planned
Closed as not planned
@mnmr

Description

@mnmr

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

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