From 2f2e10e93127880d0a6be93bf18114f646fd9443 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 30 Sep 2024 23:39:40 +0200 Subject: [PATCH] fix: only extract PageObject with InferPagePRops Close #36 --- src/types.ts | 3 ++- tests/types.spec.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index e143540..8279ac9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -161,7 +161,8 @@ export type InferPageProps< > = Controller[Method] extends (...args: any[]) => any ? Simplify< Serialize< - InferProps>, string>['props']> & SharedProps + InferProps>, PageObject>['props']> & + SharedProps > > : never diff --git a/tests/types.spec.ts b/tests/types.spec.ts index 7bc2866..67d14ed 100644 --- a/tests/types.spec.ts +++ b/tests/types.spec.ts @@ -130,4 +130,37 @@ test.group('Types', () => { type Props = InferPageProps expectTypeOf().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>().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>().toEqualTypeOf<{ foo: number }>() + }) })