Content: The `npm run generate` fails in typescript projects.

Created on 23 May 2020  路  3Comments  路  Source: nuxt/content

Version

@nuxt/content: v1.0.0
@nuxt/typescript-runtime: v0.4.0
@nuxt/typescript-build: v0.6.0
nuxt: v2.12.2

Reproduction Link

https://github.com/tanakakjs/nuxt-blog

The project generated by create-nuxt-app. (spa & typescript)

Steps to reproduce

  1. Run npm run generate
  2. The following errors occur.
17:21 Property '$content' does not exist on type 'Context'.
    15 |
    16 | export default Vue.extend({
  > 17 |   async asyncData({ $content, params, error }) {
       |                     ^
    18 |     let posts
    19 |     try {
    20 |       posts = await $content('post')

What is the right way to deal with it?

bug

Most helpful comment

Hey guys,

Types have been added in v1.0.2, so you should not encounter this error anymore.

You just have to add @nuxt/content in your types as stated here: https://content.nuxtjs.org/installation/#typescript.

Let me know if you still have an issue and you're welcome to open a PR to improve types if needed https://github.com/nuxt/content/blob/master/types/index.d.ts.

All 3 comments

I was able to get around it in the @ts-ignore comment, but I'm not sure I'm right.

export default Vue.extend({
  // @ts-ignore
  async asyncData({$content}) {
    ......
  }
})

hey @tanakakjs, I had the same issue today and when I was googling it I found your question here! 馃槅

Adding @ts-ignore is probably the simplest solution. Although I tackled it in a bit different way. I created types/nuxt.d.ts inside my project with the following content:

import { Context } from '@nuxt/types'

declare module '@nuxt/types' {
  export interface Context {
    $content(...args: any): any
  }
}

Probably it's not any better than yours because any types gives no clues for better type checks. However it successfully compiles the source code at least.

I believe we need to wait until @nuxt/content release typings...

Version

@nuxt/content: v1.0.0
@nuxt/typescript-build: v1.0.0
@nuxt/types" v0.7.6
nuxt: v2.12.2

Hey guys,

Types have been added in v1.0.2, so you should not encounter this error anymore.

You just have to add @nuxt/content in your types as stated here: https://content.nuxtjs.org/installation/#typescript.

Let me know if you still have an issue and you're welcome to open a PR to improve types if needed https://github.com/nuxt/content/blob/master/types/index.d.ts.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dolbex picture dolbex  路  3Comments

giticon picture giticon  路  3Comments

Rednas83 picture Rednas83  路  3Comments

Haeresis picture Haeresis  路  3Comments

thely picture thely  路  4Comments