-
Notifications
You must be signed in to change notification settings - Fork 471
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
usePage types not inheriting @inertiajs/core Page type #1381
Comments
I managed to override this by re-declaring + re-exporting the whole function, as it isn't an interface:
Just out of interest, was there a particular reason the generic type argument was removed from the Similarly, the
|
I have also come up with a solution to this by re-exporting, like so: import type { Page } from '@inertiajs/core'
declare module '@inertiajs/vue3' {
export declare function usePage<T>(): Page<T>
} the Page type accepts a generic |
Ah, perfect, that's a much nicer solution! Thanks @nurdism |
I'm not sure why, but this was throwing an error for me. I adjusted it and and it seems to be ok with this instead? import type { Page } from '@inertiajs/core';
declare module '@inertiajs/vue3' {
export function usePage<T>(): Page<T>
} |
This worked like a charm for me, and I combine it with my own definition for the props, like so: import type { Page, PageProps } from '@inertiajs/core';
declare module '@inertiajs/vue3' {
export function usePage<T>(): Page<T>;
}
declare global {
export interface InertiaProps extends Page<PageProps> {
flashNotifications: Array<App.FlashNotification>;
}
} And then you can use it like so: const notifications = computed(() => usePage<InertiaProps>().props.flashNotifications); And get full autocompletion for props. I'm not a typescript expert, maybe there is a better way of doing this, please let me know, but it works like a charm. |
declare module '@inertiajs/core' {
interface PageProps { // you can define global $page/usePage() props by extending/defining this interface
myGlobalProp: string[]
}
}
declare module '@inertiajs/vue3' {
export function usePage<T>(): Page<T> // the T generic will combine any type you add to it & the PageProps interface defined in @inertiajs/core
}
const page = usePage<{ myLocalProp: string }>() // the types for page.props should be { myGlobalProp: string[]; myLocalProp: string }
const page = usePage() // the types for page.props should be { myGlobalProp: string[]; } |
You just blew my mind, thank you! Much, much cleaner. |
I use it slightly differently than your example. Seems to work fine: import type { Page } from '@inertiajs/core';
declare module '@inertiajs/vue3' {
export function usePage<T>(): Page<T>
}
interface GlobalProps extends Page {
meta: {
user: LoggedInUser
},
flash: {
message: string | null,
error: string | null
}
}
interface ManageUserPageProps extends GlobalProps {
user: FullUser
available_roles: Role[]
available_permissions: string[]
} I shove all my page prop definitions into that file. Then, in my vue files: const available_roles = usePage<ManageUserPageProps>().props.available_roles; Seems to work fine. |
I forgot to copy that definition from the old type definitions when converting the adapters to TypeScript, so they were just inferred without the generic. I've created #1394 that attempts to address this; however, I can't currently get it to build. |
While T accepts a generic type, Page accepts PageProps interface. So, we need to add T extends PageProps |
I've been trying to work with typing Separately installing Is there a way to type Here's what works great if you can import from SharedProps.d.ts import type User from "@/Types/Models/User";
import type { Page, PageProps } from "@inertiajs/core";
interface SharedProps extends PageProps {
auth: {
user: User;
};
}
export default SharedProps;
declare module "@inertiajs/vue3" {
export function usePage(): Page<SharedProps>;
} The solution below sort of works with PNPM, but it less convenient because you have to manually type every instance of SharedProps.d.ts import type User from "@/Types/Models/User";
interface SharedProps{
auth: {
user: User;
};
[key: string]: any;
}
export default SharedProps;
|
import type User from "@/Types/Models/User";
import type { Page, PageProps } from "@inertiajs/core";
interface SharedProps extends PageProps {
auth: {
user: User;
};
}
export default SharedProps;
declare module "@inertiajs/vue3" {
export function usePage(): Page<SharedProps>;
} thanks @Smef, it works on me |
Solution for when working with pnpm that I figured out
|
I had this issue with the Laravel 11 Breeze React Starter Kit and I had to put this in the global.d.ts file. declare module "@inertiajs/react" {
export declare function usePage<T>(): Page<T>;
} |
I'm using something like this on Laravel 11 and Inertia V2: import type { Page } from '@inertiajs/core'
declare module '@inertiajs/core' {
interface PageProps {
auth: {
logout: {
route: string
}
}
}
}
declare module '@inertiajs/vue3' {
export declare function usePage<T>(): Page<T>
} I saved it as |
Version:
@inertiajs/vue3
version: 1.0.0Describe the problem:
@inertiajs/vue3/types/app.d.ts@usePage
types are declared asit does not inherit from the
Page
type from@inertiajs/core
and it causing all instances ofusePage()
to ignore thePageProps
interface in@inertiajs/core/types/types.ts
if extending the
PageProps
interface viait does not reflect in
usePage()
typingsThe text was updated successfully, but these errors were encountered: