question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A `store` of type `Readable<number> | undefined` generates `$store: number | undefined` even when inside a guarding `if(store)`

See original GitHub issue

Describe the bug

Let’s say we have a store: Readable<number> | undefined. Inside a guarded if (store)-block, $store should be of type number, but currently it is number | undefined.

To Reproduce

<script lang="ts">
  import { readable, Readable } from "svelte/store";

  let store: Readable<number> | undefined;

  if (!store) {
    store = readable(42);
  } else {
    const storeValue: number = $store;

    // fails with:
    // Type 'number | undefined' is not assignable to type 'number'.
    //   Type 'undefined' is not assignable to type 'number'.ts(2322)
  }
</script>

Expected behavior

The type of $store should be number when guarded with if (store).

System (please complete the following information):

"svelte-check": "^2.3.0"

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
derolfcommented, Jan 19, 2022

Btw I think it’s a regression. The problem suddenly appeared in our codebase when I returned from PTO. Some change between middle December and now?

0reactions
max24192commented, Jan 31, 2022

Your example can be rewritten by using $maybeUndefined in the #if check as well which gets rid of the error.

Then, the button would only be shown if it was enabled, because a disabled button returns false. I’d have to write

{#if maybeUndefined && $maybeUndefined !== undefined}

when I want to show a button depending on whether a store was supplied or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

type 'number | undefined' is not assignable to type 'number'. type ...
Let's say we have a store: Readable<number> | undefined . Inside a guarded if (store) -block, $store should be of type number ,...
Read more >
NuxtJS store returning local storage values as undefined
The values exist in local storage however the store returns it as undefined. If I render the values in the ui with {{value}}...
Read more >
LaTeX Error: Environment split undefined - Resurchify
For example, the align environment. The align environment is a part of amsmath package. So, if you want to use the align environment,...
Read more >
Guardianship of Adults, P-20460
A. What findings must a court make in deciding whether to appoint a guardian for an adult based on a finding of incompetence?...
Read more >
Untitled - ResearchGate
I think that Esgar creates as if making images bloom, because of the profusion ... concert halls, recording studios, record stores, SubPop label, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found