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

Time:01-19

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.

Am I wrong?

CodePudding user response:

Likely just a bug, already reported here.

I provided a workaround in your other question.

  • Related