TypeScript
A guide to using TypeScript with Sveltery Base.
Type names
Base UI uses namespaces to organize types, such as Tooltip.Root.Props. Sveltery Base exports each type by name instead, and the name joins the component and the part. Tooltip.Root.Props is TooltipRootProps. Import the types from the component's entry point, next to the component. Most parts have two core interfaces:
Props(such asTooltipRootProps)State(such asTooltipTriggerState)
Props
When creating wrapping components, you can use the Props type to accept all of the underlying Sveltery Base props for the component.
Declare a bindable prop such as open with $bindable() and pass it on with bind:, so that bind:open on your component reaches the root. Svelte's ComponentProps<typeof Tooltip.Root> gives the same props type.
State
The State type is the internal state of the component.
For example, Positioner components (such as <Popover.Positioner>) have state that describes the position of the element in relation to their anchor.
Events
Types relating to custom Sveltery Base events are also exported, named after the component.
ChangeEventDetails(such asComboboxChangeEventDetails) is the object passed to change handlers likeonValueChange.OpenChangeEventDetails(such asComboboxOpenChangeEventDetails) is the object passed toonOpenChangeon components whoseChangeEventDetailsis shared with other handlers. It extendsChangeEventDetailswithpreventUnmountOnClose().ChangeReason(such asComboboxChangeReason) is the union of possible reason strings for a change event.
Other accessible types
Depending on the component API, other types are also exported by the components. The following list is non-exhaustive, and each of these are documented where necessary.
- Popups expose imperative methods through
bind:thison the root, in place of Base UI'sactionsRefprop. For example,MenuActionsgives access to the shape of whatbind:thison<Menu.Root>returns. - The
toastobject on<Toast.Root>is a complex object with many properties.ToastObjectgives access to this interface. - Components that have a
renderprop type it as a snippet in theirPropsinterface. Sveltery Base has nouseRenderhook, so there is nouseRender.ComponentPropstype. Index a part's props instead, such asPopoverPositionerProps['render'], to reuse the snippet type.