Typescript control flow inside if/{#if} not working when using $store
See original GitHub issueWhen 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:
- Created 3 years ago
- Reactions:3
- Comments:15 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the meantime, Reassigning to a local variable can be a workaround:
It’s working for me! I can remove my re-assigment hack. Thanks a lot.