Search code examples
htmltypescriptsvelteunexpected-token

Svelte+TS type declaration inside a HTML tag attribute


Sooo. This works cause the types in TS are implicitly any.

<button on:click={() => {let modal; ...}></button>

But if I want to specify it like this:

<button on:click={() => {let modal:any; ...}></button>

it throws me a "Unexpected tokents(-1)" error.

I expected it to not throw an error.


Solution

  • The Svelte Docs list this as a limitation.

    You haven't told us what you're trying to achieve, but it sounds like you want to use Typescript in your button click handler. You could write a function within the <svelte lang="ts"> block, and reference it from your markup.