@nuxt/content: 1.7.0
nuxt: 2.14.1
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)
That TypeScript and the Nuxt type config knows these options.
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;
}
...
}
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?
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.
Most helpful comment
I will look into this