Typescript: asyncData doesn't add the return variables to CombinedVueInstance (this)

Created on 17 Jun 2020  路  3Comments  路  Source: nuxt/typescript

Describe the bug
When an object is returned by asyncData, it is not added to CombinedVueInstance (like data() does). An error is thrown when ts type checking

import Vue from 'vue'
import { PoliticalMarket } from '@/entities/political.market.model'
export default Vue.extend({
  asyncData({ params }): { id: string } {
    return {
      id: params.id
    }
  },
  mounted() {
    console.log(this.id)
  }
})
````

```bash
ERROR: Property 'id' does not exist on type 'CombinedVueInstance<Vue, ... , Readonly<Record<never, any>>>'.

Expected behavior
Should add the property to CombinedVueInstance, like data() does

Screenshots
Screenshot 2020-06-17 at 12 14 26

Screenshot 2020-06-17 at 12 14 47

Most helpful comment

You'd need to duplicate those properties in data object.

data() {
  return {
    id: '',
  }
}

All 3 comments

You'd need to duplicate those properties in data object.

data() {
  return {
    id: '',
  }
}

@xavism Sorry, there is no support for this, Nuxt asyncData can't extend CombinedVueInstance which is a Vue complex type that already auto infers types from data, computed, etc...

As @rchl just stated, you need to set defaults and declare your types in data :

export default Vue.extend({
  data() {
    return {
      id: '', // auto infers as `string` type,
      nullableString: null as string | null,
      someArrayOfStrings: [] as string[]
    }
  }
})

Perfect then! thank you both @rchl @kevinmarrec

Was this page helpful?
0 / 5 - 0 ratings