i remember a problem i had in java i created a new class that extend a not typed class , i added field to it then use it
Apr 9, 2021
5
Level 1
How to strongly type inertia.js usePage Hook
usePage is hook to to access shared data on numerous pages But that Data it's not Type How to make it strongly typed this is normal javascript without type const { user,jetstream,errorBags } = usePage().props; how to tell type script that usePage().props contain those data
ShareInertiaData.php
class ShareInertiaData
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param callable $next
* @return \Illuminate\Http\Response
*/
public function handle($request, $next)
{
Inertia::share(array_filter([
'jetstream' => function () use ($request) {
return [
'canCreateTeams' => $request->user() &&
Jetstream::hasTeamFeatures() &&
Gate::forUser($request->user())->check('create', Jetstream::newTeamModel()),
'canManageTwoFactorAuthentication' => Features::canManageTwoFactorAuthentication(),
'canUpdatePassword' => Features::enabled(Features::updatePasswords()),
'canUpdateProfileInformation' => Features::canUpdateProfileInformation(),
'flash' => $request->session()->get('flash', []),
'hasAccountDeletionFeatures' => Jetstream::hasAccountDeletionFeatures(),
'hasApiFeatures' => Jetstream::hasApiFeatures(),
'hasTeamFeatures' => Jetstream::hasTeamFeatures(),
'hasTermsAndPrivacyPolicyFeature' => Jetstream::hasTermsAndPrivacyPolicyFeature(),
'managesProfilePhotos' => Jetstream::managesProfilePhotos(),
];
},
'user' => function () use ($request) {
if (! $request->user()) {
return;
}
if (Jetstream::hasTeamFeatures() && $request->user()) {
$request->user()->currentTeam;
}
return array_merge($request->user()->toArray(), array_filter([
'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams() : null,
]), [
'two_factor_enabled' => ! is_null($request->user()->two_factor_secret),
]);
},
'errorBags' => function () {
return collect(optional(Session::get('errors'))->getBags() ?: [])->mapWithKeys(function ($bag, $key) {
return [$key => $bag->messages()];
})->all();
},
]));
return $next($request);
}
}
welcome.tsx
const Welcome = (props:React.FC) => {
console.log(usePage().props);
const { user } = usePage().props;
return (
Please or to participate in this conversation.