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>
import Counter from "$components/Counter.svelte"
import { createSWR } from "sswr"
interface Post {
id: number
userId: number
title: string
body: string
}
const swr = 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") ?
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")
?