language-tools: slot props type when reference block scope variable

Describe the bug when slot props is referencing a block scope variable it would have any type in the consumer

To Reproduce Steps to reproduce the behavior:

Component.svelte

<script>
    let list = ['']
</script>

{#each list as value}
  <slot props={value} />
{/each}

consumer

<Component let:name></Component>

where name has a type of any

Expected behavior have correct type

Screenshots If applicable, add screenshots to help explain your problem. 圖片 圖片

System (please complete the following information):

  • OS: Windows
  • IDE: VSCode
  • Plugin/Package: Svelte for VSCode, svelte2tsx

Additional context got transform to

<></>;function render() { 
 let list = [''];
 ; <>
 
 {(list).map((value) => <>
 
 <slot props={value} />
 </>)}</> return { props: {}, slots: {default: {props:value}} }} export default class { $$prop_def = __sveltets_partial(render().props) $$slot_def = render().slots }

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

Maybe this could really work in a generic way:

  • If you enter a #each, do (__svelte_ts_unwrappAttr(list))
  • If you enter a #await, do the same but with __svelte_ts_unwrapPromiseLike(promise)
  • If they use destructuring, wrap it with that immediately invoked function @jasonlyu123 proposed
  • Do this recursivly if needed

I think we can still provide the way to manually type components even if @PatrickG’s solution is easier to implement. It can serve as a fallback for edge-case, bug, or even manually type the event.

interface ComponentEvent {
    on(event: 'click', handler: (e: MouseEvent) => any): void
    on(event: 'some-event', handler: (e: CustomEvent<number>) => void): void
}

And we also need to check if the expression references another slot props. The whole process it’s so tedious I think it’s better off just don’t support it and provide a way to let the user manually defined its type.