nuxt-modules / nuxt-modules/strapi

Nuxt 3 + Strapi 4 + TypeScript error

Open
#461 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
TypeScript
Stars
717
Forks
89
Avg merge
7h 3m
Merged PRs (30d)
6

Description

Hello there!

I am fairly new to TypeScript and I wanted to create a Nuxt 3 app with Strapi 4 with TypeScript. On Strapi I have plugins "strapi-plugin-transformer" for flattening my response and "shemas-to-ts" for generating TS types. So far everything worked quite well but now I have been stuck for a while on, I bet pretty simple stuff. I have single type in Strapi called Header and I am trying to load all data on FE. I took the generated type from Strapi which looks like this:

export interface Header_Plain {
    id: number;
    createdAt: Date;  updatedAt: Date;  publishedAt?: Date;  logo: Media_Plain;
    Link: HeaderLink_Plain[];
    locale: string;
    localizations?: Header_Plain[];
}

then, in my Nuxt, I have this code:

<script setup lang="ts">
const { find } = useStrapi()

const headerData = ref<Header_Plain | null>(null)

const { data, execute } = await useAsyncData(
  'header',
  () => find<Header_Plain>('header', { populate: 'deep' })
)

function loadData() {
  execute()

  headerData.value = data.value?.data
}

onMounted(() => {
  loadData()
})
</script>

The problem I have is, that I get the data from BE but in my loadData function I am getting Vue: Type Strapi4ResponseData<Header_Plain>[] is not assignable to type error. I know that I probably should use findOne instead of find but when I use findOne the problem still occurs. Could anyone help me with this? I simply don't understand why my BE data are wrapped in this Strapi4ResponseData or how I should take them out.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the useStrapi find and findOne return types alongside the generated Header_Plain declaration and the assignment in loadData. Reproduce the TypeScript error with the shown Nuxt 3 and Strapi 4 setup, then verify that the returned data shape and the declared ref type agree.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxt, typescript
Domain
api, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.