Home > front end >  Deconstruct API response with typescript
Deconstruct API response with typescript

Time:01-08

How do I deconstruct this API response with Typescript based on best practice?

<script setup lang="ts">
// Setup Directus data fetching
const { $directus } = useNuxtApp()
import { Bestyrelse } from "~/types/bestyrelse.interface"

// Fetch bestyrelse data
const { data: bestyrelse }: Bestyrelse[] = await $directus.items("bestyrelse").readMany()
</script>

I'm getting Property 'data' does not exist on type 'Bestyrelse[]'

CodePudding user response:

const { data: bestyrelse }: Bestyrelse[] means { data: bestyrelse } is of type Bestyrelse[], which is obviously not an array. I guess you mean data is Bestyrelse[], so it will be like this:

const { data: bestyrelse }: { data: Bestyrelse[] }
  •  Tags:  
  • Related