-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
MetaFunction
type infers data
and parentsData
types from …
…loaders (#4022) * feat: metafunction type can now infer `data` and `parentsData` types from loaders * Create stupid-houses-sing.md * docs(conventions): explanation and example for `MetaFunction` type inference
- Loading branch information
Showing
3 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
"remix": minor | ||
"@remix-run/serve": minor | ||
"@remix-run/server-runtime": minor | ||
--- | ||
|
||
`MetaFunction` type can now infer `data` and `parentsData` types from loaders | ||
|
||
For example, if this meta function is for `/sales/customers/$customerId`: | ||
|
||
```ts | ||
// app/root.tsx | ||
const loader = () => { | ||
return json({ hello: "world" } as const) | ||
} | ||
export type Loader = typeof loader | ||
|
||
// app/routes/sales.tsx | ||
const loader = () => { | ||
return json({ salesCount: 1074 }) | ||
} | ||
export type Loader = typeof loader | ||
|
||
// app/routes/sales/customers.tsx | ||
const loader = () => { | ||
return json({ customerCount: 74 }) | ||
} | ||
export type Loader = typeof loader | ||
|
||
// app/routes/sales/customers/$customersId.tsx | ||
import type { Loader as RootLoader } from "../../../root" | ||
import type { Loader as SalesLoader } from "../../sales" | ||
import type { Loader as CustomersLoader } from "../../sales/customers" | ||
|
||
const loader = () => { | ||
return json({ name: "Customer name" }) | ||
} | ||
|
||
const meta: MetaFunction<typeof loader, { | ||
"root": RootLoader | ||
"routes/sales": SalesLoader, | ||
"routes/sales/customers": CustomersLoader, | ||
}> = ({ data, parentsData }) => { | ||
const { name } = data | ||
// ^? string | ||
const { customerCount } = parentsData["routes/sales/customers"] | ||
// ^? number | ||
const { salesCount } = parentsData["routes/sales"] | ||
// ^? number | ||
const { hello } = parentsData["root"] | ||
// ^? "world" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters