Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics attribute with const modifier on script tag do not type check props #2107

Closed
Nick-Mazuk opened this issue Jul 22, 2023 · 0 comments
Closed
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@Nick-Mazuk
Copy link

Describe the bug

When you have a generics attribute on a script tag (introduced in #2020), it turns off type checking anytime that generic is used as the type of a prop.

Reproduction

Let's say you have a component items.svelte

<script lang="ts" generics="const T extends readonly string[]">
    export let items: T
    export let currentItem: T[number]
</script>

You can now use this component with any nonsense props. For instance, all of the following pass type checking when none of them should:

<script lang='ts'>
    import Items from './items.svelte'
</script>

<!-- here currentItem is invalid -->
<Items items="{['item 1', 'item 2']}" currentItem="foobar" />
<Items items="{['item 1', 'item 2']}" currentItem="{3}" />
<Items items="{['item 1', 'item 2']}" currentItem="{{ foo: 'bar' }}" />

<!-- here items is invalid -->
<Items items="{['item 1', 'item 2', { foo: 'bar' }]}" currentItem="item 1" />
<Items items="{{ foo: 'bar' }}" currentItem="item 1" />
<Items items="foo bar" currentItem="item 1" />

Expected behaviour

None of the above reproduction cases should type check.

However the following case should type check:

<Items items="{['item 1', 'item 2']}" currentItem="item 2" />

Here's the equivalent TypeScript code in the TypeScript playground.

System Info

  • OS: macOS
  • IDE: VS Code (also reproduced using svelte-check CLI directly)

Relevant packages:

  • svelte: 4.0.4
  • svelte-check: 3.4.4
  • typescript 5.1.6

Which package is the issue about?

svelte-check

Additional Information, eg. Screenshots

No response

@Nick-Mazuk Nick-Mazuk added the bug Something isn't working label Jul 22, 2023
@jasonlyu123 jasonlyu123 changed the title Generics attribute on script tag do not type check props Generics attribute with const modifier on script tag do not type check props Jul 23, 2023
dummdidumm pushed a commit that referenced this issue Aug 10, 2023
#2107

Switching to let TypeScript parse the generic attribute by wrapping it with an arrow function.
@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

2 participants