Skip to content

Svelte 5: two TS quirks #2365

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

Closed
frederikhors opened this issue May 4, 2024 · 1 comment
Closed

Svelte 5: two TS quirks #2365

frederikhors opened this issue May 4, 2024 · 1 comment
Labels
question A user question

Comments

@frederikhors
Copy link
Contributor

frederikhors commented May 4, 2024

Describe the bug

In Svelte 4 app I was using:

{#each players || [] as player, i}
    <FormPlayer index={i} bind:player />
{/each}

Now with Svelte 5 I need to use:

{#each players || [] as player, i}
    <FormPlayer index={i} bind:player={players[i]} />
{/each}

But now player is unused and I get the ts warning:

'player' is declared but its value is never read. ts(6133)

Plus, with the latter one I get another typscript warning:

'players' is possibly 'null' or 'undefined'. ts(18049)

even if I'm already inside an {#each players}.

@frederikhors frederikhors added the bug Something isn't working label May 4, 2024
@jasonlyu123
Copy link
Member

jasonlyu123 commented May 6, 2024

'player' is declared but its value is never read. ts(6133)

{#each players || [] as _, i}
    <FormPlayer index={i} bind:player={players[i]} />
{/each}

'players' is possibly 'null' or 'undefined'. ts(18049)

This is because TypeScript doesn't narrow the type like this. It's not something we can fix. You can move the players || [] part to another derived/reactive variable so it'll always be not nullable, or use the ! operator to tell TS "I know what I'm doing this is not null" (now possible in SVelte 5): bind:player={players![i]}

@jasonlyu123 jasonlyu123 added question A user question and removed bug Something isn't working labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A user question
Projects
None yet
Development

No branches or pull requests

2 participants