Skip to content

TypeScript ​

A guide to using TypeScript with Vase UI.

Namespaces ​

The namespace of a part holds its types. The main types are:

  • Props, such as Tooltip.Root.Props
  • Emits, such as Tooltip.Root.Emits: the events of the part, in the form of defineEmits
  • State, such as Tooltip.Root.State

Each type also has a flat name, such as TooltipRootProps. The entry point of the component exports both names. A component with one part has its types on the component, such as Checkbox.Props and CheckboxProps.

Props and Emits ​

A component that wraps a part takes the types of the props and the events of the part from its Props and Emits types. Extend the flat names with /* @vue-ignore */, so that Vue gives the props and the listeners to the part. See Wrap a part.

vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
import type { TooltipRootEmits, TooltipRootProps } from '@vase-ui/vue/tooltip';

interface Props extends /* @vue-ignore */ TooltipRootProps {}
interface Emits extends /* @vue-ignore */ TooltipRootEmits {}

defineProps<Props>();
defineEmits<Emits>();
</script>

<template>
  <Tooltip.Root><slot /></Tooltip.Root>
</template>

State ​

The State type is the state of the part. For example, the state of Popover.Positioner gives the side and the alignment of the popup. The element slot gives the state to a typed function:

vue
<script setup lang="ts">
import { Popover } from '@vase-ui/vue/popover';

function popupClass(state: Popover.Positioner.State) {
  return state.open ? 'open' : 'closed';
}
</script>

<template>
  <Popover.Root>
    <Popover.Trigger>Trigger</Popover.Trigger>
    <Popover.Portal>
      <Popover.Positioner>
        <template #element="{ props, elementRef, state }">
          <div v-bind="props" :ref="elementRef" :class="popupClass(state)">
            <ul>
              <li>The popover is {{ state.open ? 'open' : 'closed' }}</li>
              <li>I am on the {{ state.side }} side of the anchor</li>
              <li>I am aligned at the {{ state.align }} of the side</li>
              <li>The anchor is {{ state.anchorHidden ? 'hidden' : 'visible' }}</li>
            </ul>
            <Popover.Popup>Popover</Popover.Popup>
          </div>
        </template>
      </Popover.Positioner>
    </Popover.Portal>
  </Popover.Root>
</template>

Events ​

The namespace of a part also has the types of its change events:

  • ChangeEventDetails, such as Combobox.Root.ChangeEventDetails: the details that a change listener, such as valueChange, gets.
  • OpenChangeEventDetails, such as Combobox.Root.OpenChangeEventDetails: the details that openChange gets, on a part whose ChangeEventDetails serves other listeners.
  • ChangeEventReason, such as Combobox.Root.ChangeEventReason: the union of the reasons of a change.
vue
<script setup lang="ts">
import { Combobox } from '@vase-ui/vue/combobox';

const fruits = ['Apple', 'Banana', 'Orange'];

function onValueChange(value: string | null, eventDetails: Combobox.Root.ChangeEventDetails) {
  const reason: Combobox.Root.ChangeEventReason = eventDetails.reason;
  console.log(value, reason);
}

function onOpenChange(open: boolean, eventDetails: Combobox.Root.OpenChangeEventDetails) {
  console.log(open, eventDetails);
}
</script>

<template>
  <Combobox.Root
    :items="fruits"
    default-value="Apple"
    @value-change="onValueChange"
    @open-change="onOpenChange"
  >
    <Combobox.Input />
  </Combobox.Root>
</template>

Other accessible types ​

A part or a utility can export other types. Its page documents them. Some examples:

  • A template ref on a part has the types of the members that the part exposes. useTemplateRef gives the ref of Menu.Root the type of its close method:

    vue
    <script setup lang="ts">
    import { useTemplateRef } from 'vue';
    import { Menu } from '@vase-ui/vue/menu';
    
    const menu = useTemplateRef('menu');
    
    function close() {
      menu.value?.close();
    }
    </script>
    
    <template>
      <button type="button" @click="close">Close the menu</button>
      <Menu.Root ref="menu">
        <Menu.Trigger>Trigger</Menu.Trigger>
      </Menu.Root>
    </template>
  • ToastObject is the type of a toast of Toast.Root.

  • ElementSlotProps<State> of @vase-ui/vue/types is the type of the props of the element slot. A component of yours can have the same slot. See useElement.

Generic components ​

A part whose types follow a value or a payload is a generic component. Its type parameter comes from its props:

  • A part with a value, such as RadioGroup, ToggleGroup, Slider.Root, Select.Root or Accordion.Root, takes it from v-model or default-value.
  • A popup root and its trigger, such as Tooltip.Root and Tooltip.Trigger, take the type of their payload from handle.

In this sample, the type of the listener follows default-value: a number for one thumb, and an array of numbers for a range. The type check refuses a listener with another type.

vue
<script setup lang="ts">
import { Slider } from '@vase-ui/vue/slider';

function onVolumeChange(value: number) {
  console.log(value);
}

function onRangeChange(value: number[]) {
  console.log(value[0], value[1]);
}
</script>

<template>
  <Slider.Root :default-value="50" @value-change="onVolumeChange">
    <Slider.Control>
      <Slider.Track>
        <Slider.Thumb aria-label="Volume" />
      </Slider.Track>
    </Slider.Control>
  </Slider.Root>

  <Slider.Root :default-value="[25, 75]" @value-change="onRangeChange">
    <Slider.Control>
      <Slider.Track>
        <Slider.Thumb aria-label="Minimum" />
        <Slider.Thumb aria-label="Maximum" />
      </Slider.Track>
    </Slider.Control>
  </Slider.Root>
</template>

Differences from Base UI ​

Base UIVase UI
A wrapper spreads the props of the part on itA wrapper types its props and events with Props and Emits of the part and /* @vue-ignore */. Vue gives the attributes and the listeners to the part.
OpenChangeEventDetails with preventUnmountOnClose()OpenChangeEventDetails with trigger. A <Transition> keeps a closing popup in the DOM.
The types of actionsRef, such as Menu.Root.ActionsThe types of the exposed members of the part, which a template ref gets.
useRender.ComponentProps for a component with a render propElementSlotProps<State> of @vase-ui/vue/types, for a component with an element slot.