#Infer Type in ActionData

1 messages · Page 1 of 1 (latest)

wicked agate
#

i ain't sure, since im using remix typedjson, but you can do SerializeFrom<typeof action> i believe

novel kayak
#

useActionData<typeof action>()

turbid cairn
novel kayak
#

it still complaining.

explain?

#

The | means OR, so your action could return multiple different things and you need to make sure it's returning the data your toast hook expects

#

The easiest way is to keep a consistent return type across all of them

return json({
  result: submission.reply(),
  toast: null
})

and

return json({
  result: null 
  toast: { title, description, error },
})
#

then you'd give the result to conform lastResult: actionData?.result

#

and you can filter your useEfffect

useEffect(() => {
  if (actionData.toast) {
    showToast(actionData)
  }
}, [actionData, showToast])
turbid cairn
#

It's because of the JsonifyObject , I thought using useActionData<typeof action>(); would remove that?

novel kayak
#

it's not because of JsonifyObject, that's a typescript function that serializes argument types

#

you have resultYouWant | null | undefined

#

so before you pass it to useToastNavigation you need to make sure it's not going to be null or undefined

#

or you can allow useToastNavigation to accept null and undefined args

#

and handle those cases inside it

turbid cairn
novel kayak
#

toast is going to be null in one of these two normal scenarios, so I'd let the useToastNavigation hook accept null values

#

it thinks your type could be any string

#

in your action, when you set the type, use type: "error" as const

#

or you can make string union type

type IconType = "error" | "success"
type: "error" as IconType
#

and then you can set your UseToastNotificationProps to look for IconType

turbid cairn