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

Restore Vue 3 usePage generic type #1394

Merged
merged 6 commits into from
Feb 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface PageProps {
[key: string]: unknown
}

export interface Page<SharedProps = PageProps> {
export interface Page<SharedProps extends PageProps = PageProps> {
component: string
props: PageProps &
SharedProps & {
Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ type ReactComponent = ReactNode
type HeadManagerOnUpdate = (elements: string[]) => void // TODO: When shipped, replace with: Inertia.HeadManagerOnUpdate
type HeadManagerTitleCallback = (title: string) => string // TODO: When shipped, replace with: Inertia.HeadManagerTitleCallback

type AppType<SharedProps = PageProps> = FunctionComponent<
type AppType<SharedProps extends PageProps = PageProps> = FunctionComponent<
{
children?: (props: { Component: ComponentType; key: Key; props: Page<SharedProps>['props'] }) => ReactNode
} & SetupOptions<unknown, SharedProps>['props']
>

export type SetupOptions<ElementType, SharedProps> = {
export type SetupOptions<ElementType, SharedProps extends PageProps> = {
el: ElementType
App: AppType
props: {
Expand All @@ -33,7 +33,7 @@ type BaseInertiaAppOptions = {
}

type CreateInertiaAppSetupReturnType = ReactInstance | void
type InertiaAppOptionsForCSR<SharedProps> = BaseInertiaAppOptions & {
type InertiaAppOptionsForCSR<SharedProps extends PageProps> = BaseInertiaAppOptions & {
id?: string
page?: Page | string
render?: undefined
Expand All @@ -49,21 +49,21 @@ type InertiaAppOptionsForCSR<SharedProps> = BaseInertiaAppOptions & {
}

type CreateInertiaAppSSRContent = { head: string[]; body: string }
type InertiaAppOptionsForSSR<SharedProps> = BaseInertiaAppOptions & {
type InertiaAppOptionsForSSR<SharedProps extends PageProps> = BaseInertiaAppOptions & {
id?: undefined
page: Page | string
render: typeof renderToString
progress: undefined
setup(options: SetupOptions<null, SharedProps>): ReactInstance
}

export default async function createInertiaApp<SharedProps = PageProps>(
export default async function createInertiaApp<SharedProps extends PageProps = PageProps>(
options: InertiaAppOptionsForCSR<SharedProps>,
): Promise<CreateInertiaAppSetupReturnType>
export default async function createInertiaApp<SharedProps = PageProps>(
export default async function createInertiaApp<SharedProps extends PageProps = PageProps>(
options: InertiaAppOptionsForSSR<SharedProps>,
): Promise<CreateInertiaAppSSRContent>
export default async function createInertiaApp<SharedProps = PageProps>({
export default async function createInertiaApp<SharedProps extends PageProps = PageProps>({
id = 'app',
resolve,
setup,
Expand Down
6 changes: 3 additions & 3 deletions packages/vue3/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHeadManager, Page, router } from '@inertiajs/core'
import { createHeadManager, Page, PageProps, router } from '@inertiajs/core'
import { DefineComponent, defineComponent, h, markRaw, Plugin, PropType, ref, shallowRef } from 'vue'
import remember from './remember'
import { VuePageHandlerArgs } from './types'
Expand All @@ -15,7 +15,7 @@ export interface InertiaAppProps {
export type InertiaApp = DefineComponent<InertiaAppProps>

const component = ref(null)
const page = ref<Partial<Page>>({})
const page = ref<Page<any>>(null)
const layout = shallowRef(null)
const key = ref(null)
let headManager = null
Expand Down Expand Up @@ -115,6 +115,6 @@ export const plugin: Plugin = {
},
}

export function usePage() {
export function usePage<SharedProps extends PageProps>(): Page<SharedProps> {
return page.value
}
2 changes: 1 addition & 1 deletion playgrounds/vue3/resources/js/Components/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Link, usePage } from '@inertiajs/vue3'
import { computed } from 'vue'

const appName = computed(() => usePage().props.appName)
const appName = computed(() => usePage<{ appName: string }>().props.appName)
</script>

<template>
Expand Down