language-tools: Typescript control flow inside if/{#if} not working when using $store
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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 15 (7 by maintainers)
Commits related to this issue
- Tests for $store To have something to test against when tackling #493 Note that the ranges in these tests are wrong (they end one too soon) (this is a known limitation currently) so if the tests fail... — committed to dummdidumm/language-tools by deleted user 4 years ago
- Tests for $store (#605) To have something to test against when tackling #493 Note that the ranges in these tests are wrong (they end one too soon) (this is a known limitation currently) so if the te... — committed to sveltejs/language-tools by dummdidumm 4 years ago
- (feat) control flow for stores #493 $store --> (__svelte_store_get(store), $store) By using the comma operand we allow TypeScript's control flow to work. Before that, it was $store --> __svelte_store... — committed to dummdidumm/language-tools by deleted user 4 years ago
- (feat) control flow for stores (#719) #493 $store --> (__svelte_store_get(store), $store) By using the comma operand we allow TypeScript's control flow to work. Before that, it was $store --> __sve... — committed to sveltejs/language-tools by dummdidumm 3 years ago
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.
Of course. Just wanted to make sure its not a duplicate before opening it 😃