Skip to content

Commit

Permalink
[2.x] Declare global types for React PageProps (#405)
Browse files Browse the repository at this point in the history
* declare types for page props

* Also for JSX

* missing import

* fix TS2310

* include "skipLibCheck"
  • Loading branch information
matsantosz authored Sep 11, 2024
1 parent 2bdef9f commit 8f4176b
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import ApplicationLogo from '@/Components/ApplicationLogo';
import Dropdown from '@/Components/Dropdown';
import NavLink from '@/Components/NavLink';
import ResponsiveNavLink from '@/Components/ResponsiveNavLink';
import { Link } from '@inertiajs/react';
import { User } from '@/types';
import { Link, usePage } from '@inertiajs/react';

export default function Authenticated({ header, children }: PropsWithChildren<{ header?: ReactNode }>) {
const user = usePage().props.auth.user;

export default function Authenticated({ user, header, children }: PropsWithChildren<{ user: User, header?: ReactNode }>) {
const [showingNavigationDropdown, setShowingNavigationDropdown] = useState(false);

return (
Expand Down
4 changes: 1 addition & 3 deletions stubs/inertia-react-ts/resources/js/Pages/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head } from '@inertiajs/react';
import { PageProps } from '@/types';

export default function Dashboard({ auth }: PageProps) {
export default function Dashboard() {
return (
<AuthenticatedLayout
user={auth.user}
header={<h2 className="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">Dashboard</h2>}
>
<Head title="Dashboard" />
Expand Down
3 changes: 1 addition & 2 deletions stubs/inertia-react-ts/resources/js/Pages/Profile/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import UpdateProfileInformationForm from './Partials/UpdateProfileInformationFor
import { Head } from '@inertiajs/react';
import { PageProps } from '@/types';

export default function Edit({ auth, mustVerifyEmail, status }: PageProps<{ mustVerifyEmail: boolean, status?: string }>) {
export default function Edit({ mustVerifyEmail, status }: PageProps<{ mustVerifyEmail: boolean, status?: string }>) {
return (
<AuthenticatedLayout
user={auth.user}
header={<h2 className="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">Profile</h2>}
>
<Head title="Profile" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import TextInput from '@/Components/TextInput';
import { Link, useForm, usePage } from '@inertiajs/react';
import { Transition } from '@headlessui/react';
import { FormEventHandler } from 'react';
import { PageProps } from '@/types';

export default function UpdateProfileInformation({ mustVerifyEmail, status, className = '' }: { mustVerifyEmail: boolean, status?: string, className?: string }) {
const user = usePage<PageProps>().props.auth.user;
const user = usePage().props.auth.user;

const { data, setData, patch, errors, processing, recentlySuccessful } = useForm({
name: user.name,
Expand Down
2 changes: 0 additions & 2 deletions stubs/inertia-react-ts/resources/js/ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ createServer((page) =>
// @ts-expect-error
global.route<RouteName> = (name, params, absolute) =>
route(name, params as any, absolute, {
// @ts-expect-error
...page.props.ziggy,
// @ts-expect-error
location: new URL(page.props.ziggy.location),
});

Expand Down
6 changes: 6 additions & 0 deletions stubs/inertia-react-ts/resources/js/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PageProps as InertiaPageProps } from '@inertiajs/core';
import { AxiosInstance } from 'axios';
import { route as ziggyRoute } from 'ziggy-js';
import { PageProps as AppPageProps } from './';

declare global {
interface Window {
Expand All @@ -8,3 +10,7 @@ declare global {

var route: typeof ziggyRoute;
}

declare module '@inertiajs/core' {
interface PageProps extends InertiaPageProps, AppPageProps {}
}
1 change: 1 addition & 0 deletions stubs/inertia-react-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"target": "ESNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"noEmit": true,
"paths": {
"@/*": ["./resources/js/*"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import ApplicationLogo from '@/Components/ApplicationLogo';
import Dropdown from '@/Components/Dropdown';
import NavLink from '@/Components/NavLink';
import ResponsiveNavLink from '@/Components/ResponsiveNavLink';
import { Link } from '@inertiajs/react';
import { Link, usePage } from '@inertiajs/react';

export default function Authenticated({ header, children }) {
const user = usePage().props.auth.user;

export default function Authenticated({ user, header, children }) {
const [showingNavigationDropdown, setShowingNavigationDropdown] = useState(false);

return (
Expand Down
3 changes: 1 addition & 2 deletions stubs/inertia-react/resources/js/Pages/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head } from '@inertiajs/react';

export default function Dashboard({ auth }) {
export default function Dashboard() {
return (
<AuthenticatedLayout
user={auth.user}
header={<h2 className="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">Dashboard</h2>}
>
<Head title="Dashboard" />
Expand Down
3 changes: 1 addition & 2 deletions stubs/inertia-react/resources/js/Pages/Profile/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import UpdatePasswordForm from './Partials/UpdatePasswordForm';
import UpdateProfileInformationForm from './Partials/UpdateProfileInformationForm';
import { Head } from '@inertiajs/react';

export default function Edit({ auth, mustVerifyEmail, status }) {
export default function Edit({ mustVerifyEmail, status }) {
return (
<AuthenticatedLayout
user={auth.user}
header={<h2 className="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">Profile</h2>}
>
<Head title="Profile" />
Expand Down

0 comments on commit 8f4176b

Please sign in to comment.