Guard for non-nullish value nested in store is reset after `#each`
See original GitHub issueDescribe the bug
Strange title I know…
Minimum reproducible example is:
The error only occurs when using a store that contains an object with a field that is optional or | null
or | undefined
and this field is an object.
After #each
ing over the foo
strings (or even writing {#each [$data.item.foo] as str}
when having only a string
) the type safety of $data.item
is reset and I get an error like in the screenshot. This is even the case when testing like {#if $data?.item?.foo}
.
Without the each
I get no error.
Reproduction
<script lang="ts">
import type { Readable } from 'svelte/store';
let data: Readable<{
item?: {
foo: string[];
};
}>;
</script>
{#if $data?.item}
{#each $data.item.foo as str}
<li>{str}</li>
{/each}
<pre>{JSON.stringify($data.item.foo)}</pre>
{/if}
Expected behaviour
Well, typescript should know that the value is still non-nuliish.
System Info
- OS: NixOS
- IDE: VSCodium with official extension Svelte for VS Code (both latest version)
Which package is the issue about?
svelte2tsx
Additional Information, eg. Screenshots
I’m not sure if the problem is with svelte2tsx
and I don’t know how to verify this.
The problem seems related to #1341
Issue Analytics
- State:
- Created a year ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Best way to build a network of Nodes from JSON to classes
I need to store them in a class of objects for storing in a Cassandra database. I have built the individual classes for...
Read more >How to handle deeply-nested nullable fields in JavaScript and ...
I have found idx to be a safe method of accessing these nested nullable values. A naive implementation of this would be: ...
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
I played around with this a little, this looks like a TypeScript bug to me. Minimum reproducible in a TS file:
I created https://github.com/microsoft/TypeScript/issues/49111, let’s see what they have to say.
Yeah. Since it’s an edge case. It probably won’t be prioritised on the typescript side. Let’s see if we can find a way to adjust all the reference-like features. Guess it can be achieved by finding the definition of the $store variable and then running the reference/rename again on the store?