Skip to contents

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 as TooltipRootProps)
  • State (such as TooltipTriggerState)

Props

When creating wrapping components, you can use the Props type to accept all of the underlying Sveltery Base props for the component.

Prop types

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.

Positioner state

Events

Types relating to custom Sveltery Base events are also exported, named after the component.

  • ChangeEventDetails (such as ComboboxChangeEventDetails) is the object passed to change handlers like onValueChange.
  • OpenChangeEventDetails (such as ComboboxOpenChangeEventDetails) is the object passed to onOpenChange on components whose ChangeEventDetails is shared with other handlers. It extends ChangeEventDetails with preventUnmountOnClose().
  • ChangeReason (such as ComboboxChangeReason) is the union of possible reason strings for a change event.
Change event types

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:this on the root, in place of Base UI's actionsRef prop. For example, MenuActions gives access to the shape of what bind:this on <Menu.Root> returns.
  • The toast object on <Toast.Root> is a complex object with many properties. ToastObject gives access to this interface.
  • Components that have a render prop type it as a snippet in their Props interface. Sveltery Base has no useRender hook, so there is no useRender.ComponentProps type. Index a part's props instead, such as PopoverPositionerProps['render'], to reuse the snippet type.