Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this object is possibly null or undefined if it's in an else if?

I’m using the below code in a component:

  • Box.svelte:
<script lang="ts">
    type T = $$Generic;

    export let loading = false;
    export let error = false;
    export let value: T | null | undefined = undefined;
</script>

<div>
    {#if loading}
        Loading...
    {:else if error}
        Error...
    {:else if value}
    <slot {value} />
    {/if}
</div>

Using it like this:

<Box
    loading={$player_service.loading}
    error={$player_service.error}
    value={$player_service.data?.player}
    let:value={player}
>
  <!-- Here player is still possibly `null` or `undefined`, why? -->

  {player.lastname}
</Box>

I expect player to be not null and not undefined because of the {:else if value} in Box.svelte.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Am I wrong?

>Solution :

Likely just a bug, already reported here.

I provided a workaround in your other question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading