You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<script>import{useSWR}from"sswr"const{data: posts}=useSWR("https://jsonplaceholder.typicode.com/posts")</script><ul>
{#each $posts as post}
<li>{post.title}</li>
{/each}
</ul>
Then typescript tells me that I can't use $posts in an {#each} block, because the type of $posts is unknown.
edit
Okay I figured it out, apparently I needed to write it like so:
<script>importCounterfrom"$components/Counter.svelte"import{createSWR}from"sswr"interfacePost{
id: number
userId: number
title: string
body: string}constswr=createSWR<Post[]>()const{data: posts}=swr.useSWR("https://jsonplaceholder.typicode.com/posts")</script><ul>
{#each $posts as post}
<li>{post.title}</li>
{/each}
</ul>
But is there a good reason why this has to go through createSWR? Why can't I define a type directly on the useSWR() function? Like const { data: posts } = useSWR<Post[]>("https://jsonplaceholder.typicode.com/posts") ?
The text was updated successfully, but these errors were encountered:
When I write this:
Then typescript tells me that I can't use
$posts
in an{#each}
block, because the type of$posts
isunknown
.edit
Okay I figured it out, apparently I needed to write it like so:
But is there a good reason why this has to go through
createSWR
? Why can't I define a type directly on theuseSWR()
function? Likeconst { data: posts } = useSWR<Post[]>("https://jsonplaceholder.typicode.com/posts")
?The text was updated successfully, but these errors were encountered: