Skip to content

Commit

Permalink
Restore Vue 3 usePage generic type (#1394)
Browse files Browse the repository at this point in the history
* Restore Vue 3 `usePage` generic type

* Constrain Page generic

* Fix type issue returning page from generic usePage

* Add types to usePage usage in Vue 3 playground

* Rename generic type for consistency

* Update React adapter for constrained Page generic
  • Loading branch information
jessarcher authored Feb 2, 2023
1 parent 1308029 commit 60b167c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
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

0 comments on commit 60b167c

Please sign in to comment.