Closed spiffytech closed 1 month ago
Latest commit: e963ad3a0bd414d0788801ba853b3d395237a438
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Currently, load functions are clumsy to use from TypeScript because the
RouteSectionProps
type marks thedata
prop as optional. If you try working around that in your component (such asComponent<Required<RouteSectionProps<MyProps>>>)
, you instead get compile errors that your component's props type isn't assignable toRouteDefinition
.The only workaround I've found is runtime null checks or type-level non-null assertions, at every call site that touches the
data
prop inside a component.This PR marks the
data
prop as non-optional: the route props will always have adata
field, set to theRouteSectionProps
generic parameter type. If not load function is supplied, the field's value would beundefined
, with a type ofunknown
.What happens if the route doesn't have a load function defined? Your component would be declared as
Component<RouteSectionProps>
. Without the type param supplied, thedata
prop receives typeunknown
, and is unusable inside your component.If a component sometimes does/doesn't receive load data, it can use
Component<RouteSectionProps<Foo | undefined>>
.This approach provides accurate types when you have a load function, and still signals the
data
prop cannot be read when you do not have a load function.