Content: Hook types incomplete

Created on 27 Aug 2020  路  8Comments  路  Source: nuxt/content

Version

@nuxt/content: 1.7.0
nuxt: 2.14.1

Steps to reproduce

When using hooks in the nuxt config TypeScript warns me for incomplete types of the hook builder by Nuxt.

//nuxt.config.ts
export default {
  modules: [,
    '@nuxt/content'
  ],
  hooks: {
    'content:file:beforeInsert': (document) => {
      if (document.extension === '.md') {
        const { time } = require('reading-time')(document.text)

        document.readingTime = time
      }
    }
  }
}

TypeScript doesn't know the content:file:beforeInsert function (and other hooks)

What is Expected?

That TypeScript and the Nuxt type config knows these options.

What is actually happening?

The Nuxt content decalrations should be expanded for hooks:
https://github.com/nuxt/content/blob/1dff4e9e9c4bcff03396d711c37f5eee70b8690c/packages/content/types/index.d.ts#L35

As a start I tried the following:

```

declare module "@nuxt/types" {
...
interface Configuration {
"content:file:beforeInsert": (document: any) => void;
}
...
}

bug

Most helpful comment

I will look into this

All 8 comments

I will look into this

I'm not sure if related but maybe it helps investigating the problem.
It looks like the type declaration file of @nuxt/content does not work. For example I also have to declare the $content function on the @nuxt/types. I have used a copy of the content types but this behavior is not wanted. I created an extra d.ts file in my project for now.

type contentFunc = (...args: Array<string | Object>) => NuxtContentInstance;

declare module "@nuxt/types" {
    interface Context {
        $content: contentFunc;
    }
}

Also the following code fixes the problem for the nuxt content hooks used in the nuxt content.

declare module "@nuxt/types/config/hooks" {
    interface NuxtOptionsHooks {
        "content:file:beforeInsert": (document: any) => void;
    }
}

Did you add @nuxt/content to your tsconfig?

And can we get better typings for document than any?

@dipsaus9 can you try #443 does this solve your Problems?

@mathe42 for some reason I can't pull the branch but the code works fine in my setup.
I used @nuxt/content, TypeScript doesn't recognizes this. Don't know if this is a bug or a TypeScript issue?

443 should fix the hook types.

I'm creating a PR that will add additional types extending PR #443. This should also fix the Database type created in PR #442

@dipsaus9 @benjamincanac can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emp1tsu picture emp1tsu  路  3Comments

davestewart picture davestewart  路  3Comments

ctwhome picture ctwhome  路  4Comments

katerlouis picture katerlouis  路  3Comments

dolbex picture dolbex  路  3Comments