Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): add missing type for head in defineNuxtComponent #25410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/nuxt/src/app/types/augments.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { UseHeadInput } from "@unhead/vue";
import type { NuxtApp, useNuxtApp } from '../nuxt'

interface NuxtStaticBuildFlags {
Expand Down Expand Up @@ -32,4 +33,11 @@ declare module 'vue' {
interface ComponentInternalInstance {
_nuxtOnBeforeMountCbs: Function[]
}
interface ComponentCustomOptions {
/**
* Available exclusively for `defineNuxtComponent`.
* It will not be executed when using `defineComponent`.
*/
head?(nuxtApp: NuxtApp): UseHeadInput
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the only issue here is that this will mark this as a valid type for defineComponent as well, right? Is there a way we can disallow that, or perhaps we could add a JSdoc comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment. I tried to extend the type exclusively for defineNuxtComponent, but this would have required copying many types from vue, as defineComponent has numerous overloads. If you have any ideas for a better approach, I am open to suggestions for improvement.

}
}
19 changes: 19 additions & 0 deletions test/fixtures/basic-types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,25 @@ describe('head', () => {
}
})
})
it('types head for defineNuxtComponent', () => {
defineNuxtComponent({
head(nuxtApp) {
expectTypeOf(nuxtApp).not.toBeAny()
return {
title: 'Site Title'
}
}
})

defineNuxtComponent({
// @ts-expect-error wrong return type for head function
head() {
return {
'test': true
}
}
})
})
})

describe('components', () => {
Expand Down