A handler in this crate hands the page a Rust value, and a React component receives JSON. Nothing in between checks that the two agree. In this chapter we'll make one Rust struct the page's props and the source of its TypeScript type, and look at the two places where the generated type and the JSON disagree.
This chapter assumes you can read a serde derive. It uses the ts-rs crate, version 12.0.1, which is not part of inertia-laravel-rust. It does not cover writing the generated files into your frontend build.
A struct is already the props
The struct InvoiceShow declared invoice_id a u64 and regarded that as the end of the conversation. The generator took it at its word and wrote invoiceId: bigint. serde wrote 7. The component compared the route's id with the prop using ===, and found on the first page load that invoice 7 was not invoice 7n.
Sometimes you write a prop's shape twice, once in Rust and once in TypeScript, and the two drift. Thankfully, render doesn't need a props! block: it takes any value that serializes to a JSON object. Take a look at Example 1-1.
Example 1-1. A struct as the page's props
#[derive(Serialize, TS)]
#[serde(rename_all = "camelCase")]
pub struct InvoiceShow {
#[ts(type = "number")]
pub invoice_id: u64,
#[ts(type = "number")]
pub total_cents: i64,
pub paid_at: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
pub async fn show(inertia: Inertia) -> inertia::Response {
inertia.render(
"Invoices/Show",
InvoiceShow { invoice_id: 7, total_cents: 1200, paid_at: None, note: None },
)
}
There are three new pieces here. #[derive(TS)] asks ts-rs for a TypeScript declaration of the struct. rename_all = "camelCase" is a serde attribute, and both serde and ts-rs follow it. #[ts(type = "number")] overrides what ts-rs would write for that one field.¹
Example 1-2 is the declaration ts-rs produces, and Example 1-3 is the page serde sends.
Example 1-2. The generated type
type InvoiceShow = { invoiceId: number, totalCents: number, paidAt: string | null, note?: string | null, };
Example 1-3. The props on the wire
{"invoiceId":7,"totalCents":1200,"paidAt":null}
As you can see, note is absent from the JSON and optional in the type. That agreement depends on #[serde(default)] sitting beside skip_serializing_if. Without default, serde still omits the field, and ts-rs writes note: string | null, which says the key is always there.
You can also change the integer mapping for the whole crate. ts-rs reads TS_RS_LARGE_INT when you build its configuration with Config::from_env(), and Config::default().with_large_int("number") does the same in code.
If you're unsure which to use, I'd set the mapping once for the crate and keep #[ts(type = ...)] for the fields that are exceptions.
What this chapter leaves open
So the struct and the type now agree for this page. Look at what made them agree: two attributes you had to know to add, on a struct that compiled and rendered correctly without them. How many of your prop structs have a 64-bit integer in them? Who on the team is expected to notice the next one?
Testing
Example 1-4 fails if the JSON gains or loses a key the type doesn't describe.
Example 1-4. Asserting the wire shape
AssertablePage::from_body(&reply.body)
.component("Invoices/Show")
.equals("invoiceId", 7)
.equals("paidAt", ())
.missing("note");
TL;DR
render accepts any Serialize value that serializes to a JSON object, so a struct can be a page's props. ts-rs generates a TypeScript type from the same struct and follows serde's renames. It maps u64 and i64 to bigint by default, and it marks a skipped field optional only when #[serde(default)] is also present; override the first and add the second.
¹ ts-rs maps i64, u64, i128 and u128 to bigint unless told otherwise. serde_json writes those values as ordinary JSON numbers.