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.

Typescript control flow inside if/{#if} not working when using $store

See original GitHub issue

When you have a store defined as follows:

import { readable, Readable } from "svelte/store"

const post: Readable<Post|null> = readable(null, /* some code that loads the post asynchronously */)

And then in your template you write:

<h1>{$post.title}</h1>

Then the typescript checker rightly complains Object is possibly 'null'. Then I was hoping that I could make that error disappear by doing this:

{#if $post !== null}
    <h1>{$post.title}</h1>
{/if}

But that has no effect whatsoever. Which kind of makes sense. I understand that typescript may not understand that that is an if-statement.

I can imagine that this would actually not be trivial to implement, but I don’t know, it would be really nice.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
AlexGalayscommented, Sep 18, 2020

In the meantime, Reassigning to a local variable can be a workaround:

$: storeValue = $store
1reaction
AlexGalayscommented, Feb 24, 2021

This should be fixed with VS Code Svelte extension version 104.4.4 and svelte-check version 1.1.36. Note that for svelte-check you also need TypeScript 4.2 or later.

Please let me know if it’s working as expected now. There are still control flow issues unrelated to store usage, these are tracked in #619 .

It’s working for me! I can remove my re-assigment hack. Thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Playground Example - Control Flow Improvements - TypeScript
Control Flow Analysis is the name for the system which narrows the potential types an identifier can be based on the code someone...
Read more >
Why does the never type not work with control flow analysis?
When directly throwing an Error in the condition, the compiler figures out that maybeString can't be null afterwards and accepts the code. I ......
Read more >
Get the best of TypeScript Control Flow Analysis - Retool
Control Flow Analysis is a core TypeScript feature that analyzes the code to get the best type inference depending on variable usages; this ......
Read more >
Type Guards and Control Flow Analysis in TypeScript 4.4
Here we have a type guard against property nested 2 levels into an object but the compiler still narrows it. However, this does...
Read more >
Graph-Based Source Code Analysis of JavaScript Repositories
lower level and global static analysis, finding control flows, and providing ... In practice it is usually employed to reveal problems undetectable with....
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