Open MarcLoupias opened 10 months ago
Notice that this problem is really annoying as there is no solution to disable a TS error in a svelte file.
I have created this github issue as a feature request because it is not something implemented that is not working as expected but something that is not implemented at all yet.
Just issued a PR addressing the type of index in each loop - please check.
As for ListgroupItemType
:
LIstgroupItem
componentTo address your CustomComponent
issue I'd suggest that instead of:
<Listgroup items={links} let:item let:index class="w-48">
<CustomComponent data="{item}" index={index}></CustomComponent>
</Listgroup>
you do the individual props in your CustomComponent
so you can call it like:
<Listgroup items={links} let:item let:index class="w-48">
<CustomComponent name={item.name} href={item.href} index={index}></CustomComponent>
</Listgroup>
FWIW, I'm getting a "Property 'name' does not exist on type 'string' error" even with the latter syntax. I can't see any way to make the buttons example from the docs type safe:
Summary
Similar issue to the
items
one.There is 2 typings issues :
index
props from the{#each ...}
svelte logic block.items
props typing whereT
is limited tostring
orListgroupItem
.I cannot pass down the
index
props from the{#each items as item, index}
:svelte-check
output this error :Error: Property 'index' does not exist on type '{ item: string | ListGroupItemType; }'. (ts)
.And for the second problem when i pass a custom type to my custom component i got a typing error for
{item}
in the loop becauseitem
type is limited tostring
orListgroupItem
:Error: Type 'string | ListGroupItemType' is not assignable to type 'CustomViewModel'. Type 'string' is not assignable to type 'CustomViewModel'. (ts)
Should be crystal clear with basic example and motivation, see below.
Basic example
Motivation
Using the list
index
in a custom component inside theListgroup
is very useful for integration or e2e testing with tools likeCypress
orPlaywright
to generate ids at runtime to allow consistent selectors writing easily.Let's say your
CustomComponent.svelte
is something like that :Motivation for using a custom type for a
CustomComponent
is obvious, it is custom so the contract between the component and the caller cannot be limited to string orListGroupItemType
, it have to be custom ;-) The contract is also the view model as theses components inside a loop are mostly dumb components.Notice that this is only a TypeScript issue, the behavior at runtime is OK.