I am using Typescript --strict
and would like to annotate types in a svelte #each
loop. Imagine something like this:
{#each data as [foo, bar], i}
It seems like
{#each data as [foo, bar]: [string[], boolean[]], i}
should work (those are the types of foo and bar respectively, but Typescript complains that the syntax is not correct. I'll likely approach the problem differently which will obviate this question, but I am curious now how to type a destructured tuple at the point of destructuring.
CodePudding user response:
Preprocessors are currently not applied to expressions within the template. If your data is typed correctly in the script block, there will be no need to type the items inline. In this case [string[], boolean[]][]
.
If data has to be typed differently for whatever reason, you could try to use a reactive statement to apply a type assertion.
E.g.
$: dataTyped = data as [string[], boolean[]][];