Skip to content

Commit

Permalink
fix: only extract PageObject with InferPagePRops
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
Julien-R44 committed Sep 30, 2024
1 parent 383c4c2 commit 2f2e10e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ export type InferPageProps<
> = Controller[Method] extends (...args: any[]) => any
? Simplify<
Serialize<
InferProps<Exclude<Awaited<ReturnType<Controller[Method]>>, string>['props']> & SharedProps
InferProps<Extract<Awaited<ReturnType<Controller[Method]>>, PageObject>['props']> &
SharedProps
>
>
: never
Expand Down
33 changes: 33 additions & 0 deletions tests/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,37 @@ test.group('Types', () => {
type Props = InferPageProps<Controller, 'index'>
expectTypeOf<Props>().toEqualTypeOf<{}>()
})

test('multiple render calls', async ({ expectTypeOf }) => {
const inertia = await new InertiaFactory().create()

class Controller {
index() {
if (Math.random() > 0.5) {
return inertia.render('foo', { bar: 1 })
}

return inertia.render('foo', { foo: 1 })
}
}

expectTypeOf<InferPageProps<Controller, 'index'>>().toEqualTypeOf<
{ bar: number } | { foo: number }
>()
})

test('ignore non-PageObject returns from controller', async ({ expectTypeOf }) => {
const inertia = await new InertiaFactory().create()

class Controller {
index() {
if (Math.random() > 0.5) return
if (Math.random() > 0.5) return { foo: 1 }

return inertia.render('foo', { foo: 1 })
}
}

expectTypeOf<InferPageProps<Controller, 'index'>>().toEqualTypeOf<{ foo: number }>()
})
})

0 comments on commit 2f2e10e

Please sign in to comment.